Skip to content

Commit 6599e32

Browse files
committed
list-ops: update to match Kotlin track
Based on v1.0.0 of the canonical data: https://github.com/exercism/problem-specifications/tree/28a32203b96ebaecac6a6968831bd66718d8ba30/exercises/list-ops Plus modifications that are being discussed in exercism/problem-specifications#826
1 parent 278e7f7 commit 6599e32

6 files changed

Lines changed: 216 additions & 281 deletions

File tree

‎config.json‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,31 +481,31 @@
481481
]
482482
},
483483
{
484-
"slug": "change",
484+
"slug": "list-ops",
485485
"difficulty": 8,
486486
"topics": [
487-
487+
488488
]
489489
},
490490
{
491-
"slug": "palindrome-products",
491+
"slug": "change",
492492
"difficulty": 8,
493493
"topics": [
494494

495495
]
496496
},
497497
{
498-
"slug": "pythagorean-triplet",
499-
"difficulty": 9,
498+
"slug": "palindrome-products",
499+
"difficulty": 8,
500500
"topics": [
501501

502502
]
503503
},
504504
{
505-
"slug": "list-ops",
505+
"slug": "pythagorean-triplet",
506506
"difficulty": 9,
507507
"topics": [
508-
508+
509509
]
510510
},
511511
{

‎exercises/list-ops/HINTS.md‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Hints
2+
3+
The `foldLeft` and `foldRight` methods are "fold" functions, which is a concept well-known in the functional programming world, but less so in the object-oriented one. See the Wikipedia page on folding for [general background](https://en.wikipedia.org/wiki/Fold_(higher-order_function)) and [signature/implementation hints](https://en.wikipedia.org/wiki/Fold_(higher-order_function)#Linear_folds).

‎exercises/list-ops/src/example/java/ListOps.java‎

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,67 @@
33
import java.util.Collection;
44
import java.util.Collections;
55
import java.util.List;
6-
import java.util.function.BiFunction;
7-
import java.util.function.BinaryOperator;
8-
import java.util.function.Predicate;
9-
import java.util.function.UnaryOperator;
6+
import java.util.function.*;
107
import java.util.stream.Collectors;
118
import java.util.stream.Stream;
129

13-
public class ListOps {
10+
class ListOps {
1411

15-
private ListOps() {
12+
static <T> List<T> append(final List<T> list1, final List<T> list2) {
13+
final List<T> result = new ArrayList<>();
14+
result.addAll(list1);
15+
result.addAll(list2);
16+
return result;
17+
}
18+
19+
static <T> List<T> concat(final List<List<T>> listOfLists) {
20+
final List<T> result = new ArrayList<>();
21+
listOfLists.forEach(result::addAll);
22+
return result;
23+
}
24+
25+
static <T> List<T> filter(final List<T> list, Predicate<T> predicate) {
26+
return list.stream().filter(predicate).collect(Collectors.toList());
1627
}
1728

18-
public static <T> int length(final List<T> list) {
29+
static <T> int size(final List<T> list) {
1930
return list.size();
2031
}
2132

22-
public static <T> List<T> reverse(final List<T> list) {
23-
List<T> result = new ArrayList(list);
33+
static <T, U> List<U> map(final List<T> list, Function<T, U> transform) {
34+
return list.stream().map(transform).collect(Collectors.toList());
35+
}
36+
37+
static <T> List<T> reverse(final List<T> list) {
38+
final List<T> result = new ArrayList<>(list);
2439
Collections.reverse(result);
2540
return result;
2641
}
2742

28-
public static <T> List<T> map(final List<T> list,
29-
UnaryOperator<T> mapper) {
30-
return list.stream().map(mapper).collect(Collectors.toList());
31-
}
43+
static <T, U> U foldLeft(final List<T> list, final U initial, final BiFunction<U, T, U> f) {
44+
if (list.isEmpty()) return initial;
3245

33-
public static <T> List<T> filter(final List<T> list,
34-
Predicate<T> predicate) {
35-
return list.stream().filter(predicate).collect(Collectors.toList());
46+
return foldLeft(
47+
new ArrayList<>(list.subList(1, list.size())),
48+
f.apply(
49+
initial,
50+
list.get(0)),
51+
f);
3652
}
3753

38-
public static <U, T> U reduce(final List<T> list,
39-
U identity,
40-
BiFunction<U, T, U> accumulator,
41-
BinaryOperator<U> combiner) {
42-
return list.stream().reduce(identity, accumulator, combiner);
54+
static <T, U> U foldRight(final List<T> list, final U initial, final BiFunction<T, U, U> f) {
55+
if (list.isEmpty()) return initial;
56+
57+
return f.apply(
58+
list.get(0),
59+
foldRight(
60+
new ArrayList<>(list.subList(1, list.size())),
61+
initial,
62+
f));
4363
}
4464

45-
public static <T> List<T> concat(final List<T>... lists) {
46-
return Stream.of(lists)
47-
.flatMap(Collection::stream)
48-
.collect(Collectors.toList());
65+
private ListOps() {
66+
// No instances.
4967
}
5068

5169
}

‎exercises/list-ops/src/main/java/.keep‎

Whitespace-only changes.
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.util.List;
2+
import java.util.function.BiFunction;
3+
import java.util.function.Function;
4+
import java.util.function.Predicate;
5+
6+
class ListOps {
7+
8+
static <T> List<T> append(List<T> list1, List<T> list2) {
9+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
10+
}
11+
12+
static <T> List<T> concat(List<List<T>> listOfLists) {
13+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
14+
}
15+
16+
static <T> List<T> filter(List<T> list, Predicate<T> predicate) {
17+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
18+
}
19+
20+
static <T> int size(List<T> list) {
21+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
22+
}
23+
24+
static <T, U> List<U> map(List<T> list, Function<T, U> transform) {
25+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
26+
}
27+
28+
static <T> List<T> reverse(List<T> list) {
29+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
30+
}
31+
32+
static <T, U> U foldLeft(List<T> list, U initial, BiFunction<U, T, U> f) {
33+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
34+
}
35+
36+
static <T, U> U foldRight(List<T> list, U initial, BiFunction<T, U, U> f) {
37+
throw new UnsupportedOperationException("Delete this statement and write your own implementation.");
38+
}
39+
40+
private ListOps() {
41+
// No instances.
42+
}
43+
44+
}

0 commit comments

Comments
 (0)