Skip to content

Commit 2380a87

Browse files
committed
Zip two trampolines.
1 parent 81f0ad0 commit 2380a87

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

core/src/main/java/fj/control/Trampoline.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
package fj.control;
22

3+
import fj.Bottom;
34
import fj.F;
45
import fj.F2;
56
import fj.P1;
67
import fj.data.Either;
78

9+
import static fj.Function.andThen;
810
import static fj.Function.curry;
911
import static fj.data.Either.left;
1012
import static fj.data.Either.right;
@@ -305,5 +307,43 @@ public Trampoline<C> f(final Trampoline<A> as, final Trampoline<B> bs) {
305307
});
306308
}
307309

308-
310+
@SuppressWarnings("LoopStatementThatDoesntLoop")
311+
public <B, C> Trampoline<C> zipWith(final Trampoline<B> b, final F2<A, B, C> f) {
312+
final Either<P1<Trampoline<A>>, A> ea = resume();
313+
final Either<P1<Trampoline<B>>, B> eb = b.resume();
314+
Trampoline<C> r;
315+
for (final P1<Trampoline<A>> x : ea.left()) {
316+
for (final P1<Trampoline<B>> y: eb.left()) {
317+
return suspend(P1.bind(x, y, new F2<Trampoline<A>, Trampoline<B>, Trampoline<C>>() {
318+
public Trampoline<C> f(final Trampoline<A> ta, final Trampoline<B> tb) {
319+
return ta.bind(tb, new F2<A, B, C>() {
320+
public C f(final A a, final B b) {
321+
return f.f(a, b);
322+
}
323+
}.curry());
324+
}
325+
}.curry()));
326+
}
327+
for (final B y: eb.right()) {
328+
return suspend(x.map(new F<Trampoline<A>, Trampoline<C>>() {
329+
public Trampoline<C> f(final Trampoline<A> ta) {
330+
return ta.map(f.flip().f(y));
331+
}
332+
}));
333+
}
334+
}
335+
for (final A x: ea.right()) {
336+
for (final B y: eb.right()) {
337+
return suspend(new P1<Trampoline<C>>() {
338+
public Trampoline<C> _1() {
339+
return pure(f.f(x, y));
340+
}
341+
});
342+
}
343+
for (final P1<Trampoline<B>> y: eb.left()) {
344+
return suspend(y.map(liftM2(f.curry()).f(pure(x))));
345+
}
346+
}
347+
throw Bottom.error("Match error: Trampoline is neither done nor suspended.");
348+
}
309349
}

0 commit comments

Comments
 (0)