Skip to content

Commit 0f86212

Browse files
authored
Merge pull request exercism#671 from exercism/list-ops-update
list-ops: update to match Kotlin track
2 parents a3136ec + 6599e32 commit 0f86212

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
@@ -497,31 +497,31 @@
497497
]
498498
},
499499
{
500-
"slug": "change",
500+
"slug": "list-ops",
501501
"difficulty": 8,
502502
"topics": [
503-
503+
504504
]
505505
},
506506
{
507-
"slug": "palindrome-products",
507+
"slug": "change",
508508
"difficulty": 8,
509509
"topics": [
510510

511511
]
512512
},
513513
{
514-
"slug": "pythagorean-triplet",
515-
"difficulty": 9,
514+
"slug": "palindrome-products",
515+
"difficulty": 8,
516516
"topics": [
517517

518518
]
519519
},
520520
{
521-
"slug": "list-ops",
521+
"slug": "pythagorean-triplet",
522522
"difficulty": 9,
523523
"topics": [
524-
524+
525525
]
526526
},
527527
{

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)