forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIteratee.java
More file actions
227 lines (184 loc) · 7.4 KB
/
Iteratee.java
File metadata and controls
227 lines (184 loc) · 7.4 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
package fj.data;
import fj.F;
import fj.F0;
import fj.F1Functions;
import fj.Function;
import fj.P;
import fj.P2;
import fj.Unit;
/**
*
*/
public final class Iteratee {
/** The input to an iteratee. */
public abstract static class Input<E> {
Input() {} // sealed
public abstract <Z> Z apply(final F0<Z> empty, final F0<F<E, Z>> el, final F0<Z> eof);
/** Input that has no values available */
public static <E> Input<E> empty() {
return new Input<E>() {
@Override
public <Z> Z apply(final F0<Z> empty, final F0<F<E, Z>> el, final F0<Z> eof) {
return empty.f();
}
};
}
/** Input that is exhausted */
public static <E> Input<E> eof() {
return new Input<E>() {
@Override
public <Z> Z apply(final F0<Z> empty, final F0<F<E, Z>> el, final F0<Z> eof) {
return eof.f();
}
};
}
/** Input that has a value available */
public static <E> Input<E> el(final E element) {
return new Input<E>() {
@Override
public <Z> Z apply(final F0<Z> empty, final F0<F<E, Z>> el, final F0<Z> eof) {
return el.f().f(element);
}
};
}
}
/** A pure iteratee computation which is either done or needs more input */
public abstract static class IterV<E, A> {
IterV() {} // sealed
/** A computation that takes an element from an input to yield a new computation */
public static <E, A> IterV<E, A> cont(final F<Input<E>, IterV<E, A>> f) {
return new IterV<E, A>() {
@Override
public <Z> Z fold(final F<P2<A, Input<E>>, Z> done, final F<F<Input<E>, IterV<E, A>>, Z> cont) {
return cont.f(f);
}
};
}
public abstract <Z> Z fold(final F<P2<A, Input<E>>, Z> done, final F<F<Input<E>, IterV<E, A>>, Z> cont);
/** A computation that has finished */
public static <E, A> IterV<E, A> done(final A a, final Input<E> i) {
final P2<A, Input<E>> p = P.p(a, i);
return new IterV<E, A>() {
@Override
public <Z> Z fold(final F<P2<A, Input<E>>, Z> done, final F<F<Input<E>, IterV<E, A>>, Z> cont) {
return done.f(p);
}
};
}
public final A run() {
final F<IterV<E, A>, Option<A>> runCont =
new F<IterV<E, A>, Option<A>>() {
final F<P2<A, Input<E>>, Option<A>> done = F1Functions.andThen(P2.__1(), Option.some_());
final F<F<Input<E>, IterV<E, A>>, Option<A>> cont = Function.constant(Option.none());
@Override
public Option<A> f(final IterV<E, A> i) {
return i.fold(done, cont);
}
};
final F<P2<A, Input<E>>, A> done = P2.__1();
final F<F<Input<E>, IterV<E, A>>, A> cont =
k -> runCont.f(k.f(Input.eof())).valueE("diverging iteratee");
return fold(done, cont);
}
/** TODO more documentation */
public final <B> IterV<E, B> bind(final F<A, IterV<E, B>> f) {
final F<P2<A, Input<E>>, IterV<E, B>> done =
xe -> {
final Input<E> e = xe._2();
final F<P2<B, Input<E>>, IterV<E, B>> done1 =
y_ -> {
final B y = y_._1();
return done(y, e);
};
final F<F<Input<E>, IterV<E, B>>, IterV<E, B>> cont =
k -> k.f(e);
final A x = xe._1();
return f.f(x).fold(done1, cont);
};
final F<F<Input<E>, IterV<E, A>>, IterV<E, B>> cont =
k -> cont(e -> k.f(e).bind(f));
return this.fold(done, cont);
}
/** An iteratee that counts and consumes the elements of the input */
public static <E> IterV<E, Integer> length() {
final F<Integer, F<Input<E>, IterV<E, Integer>>> step =
new F<Integer, F<Input<E>, IterV<E, Integer>>>() {
final F<Integer, F<Input<E>, IterV<E, Integer>>> step = this;
@Override
public F<Input<E>, IterV<E, Integer>> f(final Integer acc) {
final F0<IterV<E, Integer>> empty = () -> cont(step.f(acc));
final F0<F<E, IterV<E, Integer>>> el = () -> P.p(cont(step.f(acc + 1))).constant();
final F0<IterV<E, Integer>> eof = () -> done(acc, Input.<E>eof());
return s -> s.apply(empty, el, eof);
}
};
return cont(step.f(0));
}
/** An iteratee that skips the first n elements of the input */
public static <E> IterV<E, Unit> drop(final int n) {
final F<Input<E>, IterV<E, Unit>> step =
new F<Input<E>, IterV<E, Unit>>() {
final F<Input<E>, IterV<E, Unit>> step = this;
final F0<IterV<E, Unit>> empty = () -> cont(step);
final F0<F<E, IterV<E, Unit>>> el = () -> P.p(IterV.<E>drop(n - 1)).constant();
final F0<IterV<E, Unit>> eof = () -> done(Unit.unit(), Input.<E>eof());
@Override
public IterV<E, Unit> f(final Input<E> s) {
return s.apply(empty, el, eof);
}
};
return n == 0
? done(Unit.unit(), Input.empty())
: cont(step);
}
/** An iteratee that consumes the head of the input */
public static <E> IterV<E, Option<E>> head() {
final F<Input<E>, IterV<E, Option<E>>> step =
new F<Input<E>, IterV<E, Option<E>>>() {
final F<Input<E>, IterV<E, Option<E>>> step = this;
final F0<IterV<E, Option<E>>> empty = () -> cont(step);
final F0<F<E, IterV<E, Option<E>>>> el = () -> e -> done(Option.some(e), Input.<E>empty());
final F0<IterV<E, Option<E>>> eof = () -> done(Option.<E>none(), Input.<E>eof());
@Override
public IterV<E, Option<E>> f(final Input<E> s) {
return s.apply(empty, el, eof);
}
};
return cont(step);
}
/** An iteratee that returns the first element of the input */
public static <E> IterV<E, Option<E>> peek() {
final F<Input<E>, IterV<E, Option<E>>> step =
new F<Input<E>, IterV<E, Option<E>>>() {
final F<Input<E>, IterV<E, Option<E>>> step = this;
final F0<IterV<E, Option<E>>> empty = () -> cont(step);
final F0<F<E, IterV<E, Option<E>>>> el = () -> e -> done(Option.some(e), Input.el(e));
final F0<IterV<E, Option<E>>> eof = () -> done(Option.<E>none(), Input.<E>eof());
@Override
public IterV<E, Option<E>> f(final Input<E> s) {
return s.apply(empty, el, eof);
}
};
return cont(step);
}
/** An iteratee that consumes the input elements and returns them as a list in reverse order,
* so that the last line is the first element. This allows to build a list from 2 iteratees. */
public static <E> IterV<E, List<E>> list() {
final F<List<E>, F<Input<E>, IterV<E, List<E>>>> step =
new F<List<E>, F<Input<E>, IterV<E, List<E>>>>() {
final F<List<E>, F<Input<E>, IterV<E, List<E>>>> step = this;
@Override
public F<Input<E>, IterV<E, List<E>>> f(final List<E> acc) {
final F0<IterV<E, List<E>>> empty = () -> cont(step.f(acc));
final F0<F<E, IterV<E, List<E>>>> el = () -> e -> cont(step.f(acc.cons(e)));
final F0<IterV<E, List<E>>> eof = () -> done(acc, Input.<E>eof());
return s -> s.apply(empty, el, eof);
}
};
return cont(step.f(List.nil()));
}
}
private Iteratee() {
throw new UnsupportedOperationException();
}
}