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
355 lines (324 loc) · 11.8 KB
/
Iteratee.java
File metadata and controls
355 lines (324 loc) · 11.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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package fj.data;
import fj.F;
import fj.Function;
import fj.P;
import fj.P1;
import fj.P2;
import fj.Unit;
/**
*
*/
public final class Iteratee {
/** The input to an iteratee. */
public static abstract class Input<E> {
Input() {} // sealed
public abstract <Z> Z apply(final P1<Z> empty, final P1<F<E, Z>> el, final P1<Z> eof);
/** Input that has no values available */
public static final <E> Input<E> empty() {
return new Input<E>() {
@Override
public <Z> Z apply(final P1<Z> empty, final P1<F<E, Z>> el, final P1<Z> eof) {
return empty._1();
}
};
}
/** Input that is exhausted */
public static final <E> Input<E> eof() {
return new Input<E>() {
@Override
public <Z> Z apply(final P1<Z> empty, final P1<F<E, Z>> el, final P1<Z> eof) {
return eof._1();
}
};
}
/** Input that has a value available */
public static final <E> Input<E> el(final E element) {
return new Input<E>() {
@Override
public <Z> Z apply(final P1<Z> empty, final P1<F<E, Z>> el, final P1<Z> eof) {
return el._1().f(element);
}
};
}
}
/** A pure iteratee computation which is either done or needs more input */
public static abstract 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 = P2.<A, Input<E>>__1().andThen(Option.<A>some_());
final F<F<Input<E>, IterV<E, A>>, Option<A>> cont = Function.constant(Option.<A>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.<A, Input<E>>__1();
final F<F<Input<E>, IterV<E, A>>, A> cont =
new F<F<Input<E>, IterV<E, A>>, A>() {
@Override
public A f(final F<Input<E>, IterV<E, A>> k) {
return runCont.f(k.f(Input.<E>eof())).valueE("diverging iteratee"); //$NON-NLS-1$
}
};
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 =
new F<P2<A, Input<E>>, IterV<E, B>>() {
@Override
public IterV<E, B> f(final P2<A, Input<E>> xe) {
final Input<E> e = xe._2();
final F<P2<B, Input<E>>, IterV<E, B>> done =
new F<P2<B, Input<E>>, IterV<E, B>>() {
@Override
public IterV<E, B> f(final P2<B, Input<E>> y_) {
final B y = y_._1();
return done(y, e);
}
};
final F<F<Input<E>, IterV<E, B>>, IterV<E, B>> cont =
new F<F<Input<E>, IterV<E, B>>, IterV<E, B>>() {
@Override
public IterV<E, B> f(final F<Input<E>, IterV<E, B>> k) {
return k.f(e);
}
};
final A x = xe._1();
return f.f(x).fold(done, cont);
}
};
final F<F<Input<E>, IterV<E, A>>, IterV<E, B>> cont =
new F<F<Input<E>, IterV<E, A>>, IterV<E, B>>() {
@Override
public IterV<E, B> f(final F<Input<E>, IterV<E, A>> k) {
return cont(new F<Input<E>, IterV<E, B>>() {
@Override
public IterV<E, B> f(final Input<E> e) {
return k.f(e).bind(f);
}
});
}
};
return this.fold(done, cont);
}
/** An iteratee that counts and consumes the elements of the input */
public static final <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 P1<IterV<E, Integer>> empty =
new P1<IterV<E, Integer>>() {
@Override
public IterV<E, Integer> _1() {
return cont(step.f(acc));
}
};
final P1<F<E, IterV<E, Integer>>> el =
new P1<F<E, IterV<E, Integer>>>() {
@Override
public F<E, IterV<E, Integer>> _1() {
return P.p(cont(step.f(acc + 1))).<E>constant();
}
};
final P1<IterV<E, Integer>> eof =
new P1<IterV<E, Integer>>() {
@Override
public IterV<E, Integer> _1() {
return done(acc, Input.<E>eof());
}
};
return new F<Input<E>, IterV<E, Integer>>() {
@Override
public IterV<E, Integer> f(final Input<E> s) {
return s.apply(empty, el, eof);
}
};
}
};
return cont(step.f(0));
}
/** An iteratee that skips the first n elements of the input */
public static final <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 P1<IterV<E, Unit>> empty =
new P1<IterV<E, Unit>>() {
@Override
public IterV<E, Unit> _1() {
return cont(step);
}
};
final P1<F<E, IterV<E, Unit>>> el =
new P1<F<E, IterV<E, Unit>>>() {
@Override
public F<E, IterV<E, Unit>> _1() {
return P.p(IterV.<E>drop(n - 1)).<E>constant();
}
};
final P1<IterV<E, Unit>> eof =
new P1<IterV<E, Unit>>() {
@Override
public IterV<E, Unit> _1() {
return 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.<E>empty())
: cont(step);
}
/** An iteratee that consumes the head of the input */
public static final <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 P1<IterV<E, Option<E>>> empty =
new P1<IterV<E, Option<E>>>() {
@Override
public IterV<E, Option<E>> _1() {
return cont(step);
}
};
final P1<F<E, IterV<E, Option<E>>>> el =
new P1<F<E, IterV<E, Option<E>>>>() {
@Override
public F<E, IterV<E, Option<E>>> _1() {
return new F<E, Iteratee.IterV<E, Option<E>>>() {
@Override
public IterV<E, Option<E>> f(final E e) {
return done(Option.<E>some(e), Input.<E>empty());
}
};
}
};
final P1<IterV<E, Option<E>>> eof =
new P1<IterV<E, Option<E>>>() {
@Override
public IterV<E, Option<E>> _1() {
return 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 final <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 P1<IterV<E, Option<E>>> empty =
new P1<IterV<E, Option<E>>>() {
@Override
public IterV<E, Option<E>> _1() {
return cont(step);
}
};
final P1<F<E, IterV<E, Option<E>>>> el =
new P1<F<E, IterV<E, Option<E>>>>() {
@Override
public F<E, IterV<E, Option<E>>> _1() {
return new F<E, Iteratee.IterV<E, Option<E>>>() {
@Override
public IterV<E, Option<E>> f(final E e) {
return done(Option.<E>some(e), Input.<E>el(e));
}
};
}
};
final P1<IterV<E, Option<E>>> eof =
new P1<IterV<E, Option<E>>>() {
@Override
public IterV<E, Option<E>> _1() {
return 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 final <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 P1<IterV<E, List<E>>> empty =
new P1<IterV<E, List<E>>>() {
@Override
public IterV<E, List<E>> _1() {
return cont(step.f(acc));
}
};
final P1<F<E, IterV<E, List<E>>>> el =
new P1<F<E, IterV<E, List<E>>>>() {
@Override
public F<E, IterV<E, List<E>>> _1() {
return new F<E, Iteratee.IterV<E, List<E>>>() {
@Override
public IterV<E, List<E>> f(final E e) {
return cont(step.f(acc.cons(e)));
}
};
}
};
final P1<IterV<E, List<E>>> eof =
new P1<IterV<E, List<E>>>() {
@Override
public IterV<E, List<E>> _1() {
return done(acc, Input.<E>eof());
}
};
return new F<Input<E>, IterV<E, List<E>>>() {
@Override
public IterV<E, List<E>> f(final Input<E> s) {
return s.apply(empty, el, eof);
}
};
}
};
return cont(step.f(List.<E> nil()));
}
}
private Iteratee() {
throw new UnsupportedOperationException();
}
}