Newer
Older
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
package inf226.util.immutable;
import inf226.util.Maybe;
import inf226.util.Mutable;
import java.util.function.Function;
import java.util.function.BiFunction;
import java.util.function.Consumer;
public final class List<T> {
private final Maybe<ListItem<T> > items;
public final Maybe<T> last;
public final int length;
private List() {
this.items = Maybe.nothing();
this.last = Maybe.nothing();
this.length = 0;
}
private List(T head, List<T> tail) {
this.items = new Maybe<ListItem<T>>(new ListItem<T>(head, tail));
/* Construct a reference to the last element of
the list. */
T new_last;
try {
new_last = tail.last.get();
} catch (Maybe.NothingException e) {
new_last = head;
}
this.last = new Maybe<T>(new_last);
this.length = tail.length + 1;
}
public static<U> List<U> empty() {
return new List<U>();
}
public static<U> List<U> cons(U head, List<U> tail) {
return new List<U>(head,tail);
}
public static<U> List<U> singleton(U head) {
return new List<U>(head,empty());
}
public Maybe<T> head() {
try {
return new Maybe<T>(items.get().head);
} catch (Maybe.NothingException e) {
return Maybe.nothing();
}
}
public Maybe< List<T> > tail() {
try {
return new Maybe<List<T> >(items.get().tail);
} catch (Maybe.NothingException e) {
return Maybe.nothing();
}
}
public List<T> add(T element) {
return cons(element, this);
}
public<U> List<U> map(Function<T,U> f) {
List<U> result = empty();
try {
for(List<T> l = this.reverse(); ; l = l.tail().get()) {
result = cons(f.apply(l.head().get()), result);
}
} catch (Maybe.NothingException e) {
// No more elements
}
return result;
}
public<B,C> List<C> zipWith(List<B> other, BiFunction<T,B,C> f) {
Builder<C> result = builder();
try {
List<T> l0 = this.reverse();
List<B> l1 = other.reverse();
while(true) {
result.accept(f.apply(l0.items.get().head, l1.items.get().head));
l0 = l0.items.get().tail;
l1 = l1.items.get().tail;
}
} catch (Maybe.NothingException e) {
// No more elements
}
return result.getList();
}
@Override
public final boolean equals(Object other) {
if (other == null)
return false;
if (getClass() != other.getClass())
return false;
@SuppressWarnings("unchecked")
final List<Object> list_other = (List<Object>) other;
final Mutable<Boolean> equal = new Mutable<Boolean>(length == list_other.length);
List<Boolean> equalList = zipWith(list_other, (a, b) -> a.equals(b));
equalList.forEach(e -> { equal.accept(equal.get() && e);});
return equal.get();
}
public void forEach(Consumer<T> c) {
sequenceConsumer(c).accept(this);
}
public static<U> Consumer<List<U>> sequenceConsumer(Consumer<U> c) {
return new Consumer<List<U>>(){
@Override
public void accept(List<U> l) {
try {
for(ListItem<U> e = l.items.get(); ; e = e.tail.items.get()) {
c.accept(e.head);
}
} catch (Maybe.NothingException e) {
// No more elements
}
} };
}
public static class Builder<U> implements Consumer<U> {
private List<U> list;
public Builder() { list = empty(); }
@Override
public synchronized void accept(U element) { list = cons(element, list) ; }
public List<U> getList() { return list ; }
}
public List<T> reverse() {
List<T> result = empty();
try {
for(ListItem<T> e = this.items.get(); ; e = e.tail.items.get()) {
result = cons(e.head, result);
}
} catch (Maybe.NothingException e) {
// No more elements
}
return result;
}
public static<U> Builder<U> builder(){return new Builder<U>();}
private static class ListItem<T> {
public final T head;
public final List<T> tail;
ListItem(T head, List<T> tail) {
this.head = head;
this.tail = tail;
}
}
}