-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExample154.java
More file actions
173 lines (143 loc) · 4.8 KB
/
Example154.java
File metadata and controls
173 lines (143 loc) · 4.8 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Example 154 from page 123
//
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
class Example154 {
public static void main(String[] args) {
FunList<Integer> empty = new FunList<>(null),
list1 = cons(9, cons(13, cons(0, empty))), // 9 13 0
list2 = cons(7, list1), // 7 9 13 0
list3 = cons(8, list1), // 8 9 13 0
list4 = list1.insert(1, 12), // 9 12 13 0
list5 = list2.removeAt(3), // 7 9 13
list6 = list5.reverse(), // 13 9 7
list7 = list5.append(list5); // 7 9 13 7 9 13
System.out.println(list1);
System.out.println(list2);
System.out.println(list3);
System.out.println(list4);
System.out.println(list5);
System.out.println(list6);
System.out.println(list7);
FunList<Double> list8 = list5.map(i -> 2.5 * i); // 17.5 22.5 32.5
System.out.println(list8);
double sum = list8.reduce(0.0, (res, item) -> res + item), // 72.5
product = list8.reduce(1.0, (res, item) -> res * item); // 12796.875
System.out.println(sum);
System.out.println(product);
}
public static <T> FunList<T> cons(T item, FunList<T> list) {
return list.insert(0, item);
}
}
class FunList<T> {
final Node<T> first;
protected static class Node<U> {
public final U item;
public final Node<U> next;
public Node(U item, Node<U> next) {
this.item = item;
this.next = next;
}
}
public FunList(Node<T> xs) {
this.first = xs;
}
public FunList() {
this(null);
}
public int getCount() {
Node<T> xs = first;
int count = 0;
while (xs != null) {
xs = xs.next;
count++;
}
return count;
}
public T get(int i) {
return getNodeLoop(i, first).item;
}
// Loop-based version of getNode
protected static <T> Node<T> getNodeLoop(int i, Node<T> xs) {
while (i != 0) {
xs = xs.next;
i--;
}
return xs;
}
// Recursive version of getNode
protected static <T> Node<T> getNodeRecursive(int i, Node<T> xs) { // Could use loop instead
return i == 0 ? xs : getNodeRecursive(i-1, xs.next);
}
public static <T> FunList<T> cons(T item, FunList<T> list) {
return list.insert(0, item);
}
public FunList<T> insert(int i, T item) {
return new FunList<T>(insert(i, item, this.first));
}
protected static <T> Node<T> insert(int i, T item, Node<T> xs) {
return i == 0 ? new Node<T>(item, xs) : new Node<T>(xs.item, insert(i-1, item, xs.next));
}
public FunList<T> removeAt(int i) {
return new FunList<T>(removeAt(i, this.first));
}
protected static <T> Node<T> removeAt(int i, Node<T> xs) {
return i == 0 ? xs.next : new Node<T>(xs.item, removeAt(i-1, xs.next));
}
public FunList<T> reverse() {
Node<T> xs = first, reversed = null;
while (xs != null) {
reversed = new Node<T>(xs.item, reversed);
xs = xs.next;
}
return new FunList<T>(reversed);
}
public FunList<T> append(FunList<T> ys) {
return new FunList<T>(append(this.first, ys.first));
}
protected static <T> Node<T> append(Node<T> xs, Node<T> ys) {
return xs == null ? ys : new Node<T>(xs.item, append(xs.next, ys));
}
@Override
@SuppressWarnings("unchecked")
public boolean equals(Object that) {
return equals((FunList<T>)that); // Unchecked cast
}
public boolean equals(FunList<T> that) {
return that != null && equals(this.first, that.first);
}
// Could be replaced by a loop
protected static <T> boolean equals(Node<T> xs1, Node<T> xs2) {
return xs1 == xs2
|| xs1 != null && xs2 != null && xs1.item == xs2.item && equals(xs1.next, xs2.next);
}
public <U> FunList<U> map(Function<T,U> f) {
return new FunList<U>(map(f, first));
}
protected static <T,U> Node<U> map(Function<T,U> f, Node<T> xs) {
return xs == null ? null : new Node<U>(f.apply(xs.item), map(f, xs.next));
}
public <U> U reduce(U x0, BiFunction<U,T,U> op) {
return reduce(x0, op, first);
}
// Could be replaced by a loop
protected static <T,U> U reduce(U x0, BiFunction<U,T,U> op, Node<T> xs) {
return xs == null ? x0 : reduce(op.apply(x0, xs.item), op, xs.next);
}
// This loop is an optimized version of a tail-recursive function
public void forEach(Consumer<T> cons) {
Node<T> xs = first;
while (xs != null) {
cons.accept(xs.item);
xs = xs.next;
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
forEach(item -> sb.append(item).append(" "));
return sb.toString();
}
}