-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotEmptyList.java
More file actions
51 lines (42 loc) · 1.26 KB
/
NotEmptyList.java
File metadata and controls
51 lines (42 loc) · 1.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
public class NotEmptyList<T> implements Listuse<T> {
private T head;
private Listuse<T> tail;
NotEmptyList(T head, Listuse<T> tail) {
this.head = head;
this.tail = tail;
}
@Override
public int getListSize() {
return 1 + tail.getListSize();
}
@Override
public Listuse<T> filter(Predicate<T> predicate) {
if (predicate.test(head)) {
return new NotEmptyList<>(head, tail.filter(predicate));
}
else {
return tail.filter(predicate);
}
}
@Override
public <R> Listuse<R> map(Function<T, R> changeFunction) {
return new NotEmptyList<>(changeFunction.apply(head), tail.map(changeFunction));
}
@Override
public Listuse<T> addFront(T element) {
return new NotEmptyList<>(element, this);
}
@Override
public <R> R fold(R initial, BiFunction<R, T, R> accumulate) {
return this.tail.fold(accumulate.apply(initial, head), accumulate);
}
@Override
public void forEach(Consumer<T> action) {
action.accept(this.head);
this.tail.forEach(action);
}
}