Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions core/src/main/java/fj/control/Trampoline.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public <R> R fold(final F<Normal<A>, R> n,
// The monadic bind constructs a new Codense whose subcomputation is still `sub`, and Kleisli-composes the
// continuations.
public <B> Trampoline<B> bind(final F<A, Trampoline<B>> f) {
return codense(sub, o -> suspend(P.lazy(() -> cont.f(o).bind(f))));
return codense(sub, o -> suspend(() -> cont.f(o).bind(f)));
}

// The resumption of a Codense is the resumption of its subcomputation. If that computation is done, its result
Expand Down Expand Up @@ -126,6 +126,16 @@ public static <A> Trampoline<A> pure(final A a) {
return new Pure<>(a);
}

/**
* Suspends the given computation in a thunk.
*
* @param a A trampoline suspended in a thunk.
* @return A trampoline whose next step runs the given thunk.
*/
public static <A> Trampoline<A> suspend(final F0<Trampoline<A>> a) {
return new Suspend<>(P.lazy(a));
}

/**
* Suspends the given computation in a thunk.
*
Expand All @@ -136,6 +146,7 @@ public static <A> Trampoline<A> suspend(final P1<Trampoline<A>> a) {
return new Suspend<>(a);
}


/**
* @return The first-class version of `suspend`.
*/
Expand Down Expand Up @@ -255,15 +266,15 @@ public final <B, C> Trampoline<C> zipWith(final Trampoline<B> b, final F2<A, B,
final Either<P1<Trampoline<B>>, B> eb = b.resume();
for (final P1<Trampoline<A>> x : ea.left()) {
for (final P1<Trampoline<B>> y : eb.left()) {
return suspend(x.bind(y, F2Functions.curry((ta, tb) -> suspend(P.lazy(() -> ta.zipWith(tb, f))))));
return suspend(x.bind(y, F2Functions.curry((ta, tb) -> suspend(() -> ta.zipWith(tb, f)))));
}
for (final B y : eb.right()) {
return suspend(x.map(ta -> ta.map(F2Functions.f(F2Functions.flip(f), y))));
}
}
for (final A x : ea.right()) {
for (final B y : eb.right()) {
return suspend(P.lazy(() -> pure(f.f(x, y))));
return suspend(() -> pure(f.f(x, y)));
}
for (final P1<Trampoline<B>> y : eb.left()) {
return suspend(y.map(liftM2(F2Functions.curry(f)).f(pure(x))));
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/java/fj/data/DList.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,6 @@ public DList<A> append(DList<A> other) {
}

private static <A,B,C> F<A,Trampoline<C>> kleisliTrampCompose(F<B,Trampoline<C>> bc, F<A,Trampoline<B>> ab) {
return (A a) -> ab.f(a).bind((B b) -> Trampoline.suspend(P.lazy(() -> bc.f(b))));
return (A a) -> ab.f(a).bind((B b) -> Trampoline.suspend(() -> bc.f(b)));
}
}
6 changes: 3 additions & 3 deletions core/src/main/java/fj/data/Eval.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ private static final class PureTrampolineEval<A> extends TrampolineEval<A> {

@Override
protected final Trampoline<A> trampoline() {
return Trampoline.suspend(P.lazy(() -> Trampoline.pure(start.value())));
return Trampoline.suspend(() -> Trampoline.pure(start.value()));
}
}

Expand All @@ -219,7 +219,7 @@ private static final class BindTrampolineEval<A, B> extends TrampolineEval<B> {

@Override
protected final Trampoline<B> trampoline() {
return Trampoline.suspend(P.lazy(() -> next.trampoline().bind(v -> f.f(v).asTrampoline().trampoline())));
return Trampoline.suspend(() -> next.trampoline().bind(v -> f.f(v).asTrampoline().trampoline()));
}
}

Expand All @@ -232,7 +232,7 @@ private static final class DeferEval<A> extends TrampolineEval<A> {

@Override
protected final Trampoline<A> trampoline() {
return Trampoline.suspend(P.lazy(() -> memo._1().asTrampoline().trampoline()));
return Trampoline.suspend(() -> memo._1().asTrampoline().trampoline());
}
}
}
2 changes: 1 addition & 1 deletion core/src/main/java/fj/data/List.java
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ public final <B> B foldRight(final F2<A, B, B> f, final B b) {
* @return A Trampoline containing the final result after the right-fold reduction.
*/
public final <B> Trampoline<B> foldRightC(final F2<A, B, B> f, final B b) {
return Trampoline.suspend(P.lazy(() -> isEmpty() ? Trampoline.pure(b) : tail().foldRightC(f, b).map(F2Functions.f(f, head()))));
return Trampoline.suspend(() -> isEmpty() ? Trampoline.pure(b) : tail().foldRightC(f, b).map(F2Functions.f(f, head())));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/fj/data/State.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import fj.Unit;
import fj.control.Trampoline;

import static fj.P.lazy;
import static fj.P.p;
import static fj.control.Trampoline.suspend;
import static fj.data.List.cons;
Expand Down Expand Up @@ -75,7 +74,7 @@ public static <S, A, B> State<S, List<B>> traverse(List<A> list, F<A, State<S, B
}

private static <S, A> State<S, A> suspended(F<S, Trampoline<P2<S, A>>> runF) {
return new State<>(s -> suspend(lazy(() -> runF.f(s))));
return new State<>(s -> suspend(() -> runF.f(s)));
}

private final F<S, Trampoline<P2<S, A>>> runF;
Expand Down
5 changes: 2 additions & 3 deletions quickcheck/src/main/java/fj/test/Gen.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import static fj.Function.flip;
import static fj.Monoid.intAdditionMonoid;
import static fj.Ord.intOrd;
import static fj.P.lazy;
import static fj.P2.__1;
import static fj.control.Trampoline.pure;
import static fj.control.Trampoline.suspend;
Expand Down Expand Up @@ -551,15 +550,15 @@ final class Tramp {

// Picks elements in constant stack space
private Trampoline<List<A>> tramp(List<A> remainAs, int remainN, int remainALength) {
return suspend(lazy(() ->
return suspend(() ->
(remainN == 0) ?
// We have picked N elements; stop
pure(nil()) :
// For M remaining elements of which N will be picked, pick remainAs.head() with probability N/M
(r.choose(0, remainALength - 1) < remainN) ?
tramp(remainAs.tail(), remainN - 1, remainALength - 1)
.map(pickedTail -> cons(remainAs.head(), pickedTail)) :
tramp(remainAs.tail(), remainN, remainALength - 1)));
tramp(remainAs.tail(), remainN, remainALength - 1));
}

}
Expand Down