Package fj.control
Class Trampoline<A>
- java.lang.Object
-
- fj.control.Trampoline<A>
-
- Direct Known Subclasses:
Trampoline.Codense
,Trampoline.Normal
public abstract class Trampoline<A> extends java.lang.Object
A Trampoline is a potentially branching computation that can be stepped through and executed in constant stack. It represents suspendable coroutines with subroutine calls, reified as a data structure.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static class
Trampoline.Codense<A>
private static class
Trampoline.Normal<A>
private static class
Trampoline.Pure<A>
private static class
Trampoline.Suspend<A>
-
Constructor Summary
Constructors Constructor Description Trampoline()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description <B> Trampoline<B>
apply(Trampoline<F<A,B>> lf)
Performs function application within a Trampoline (applicative functor pattern).<B,C>
Trampoline<C>bind(Trampoline<B> lb, F<A,F<B,C>> f)
Binds the given function across the result of this Trampoline and the given Trampoline.abstract <B> Trampoline<B>
bind(F<A,Trampoline<B>> f)
Binds the given continuation to the result of this trampoline.static <A,B>
F<F<A,Trampoline<B>>,F<Trampoline<A>,Trampoline<B>>>bind_()
private static <A,B>
Trampoline.Codense<B>codense(Trampoline.Normal<A> a, F<A,Trampoline<B>> k)
protected abstract <R> R
fold(F<Trampoline.Normal<A>,R> n, F<Trampoline.Codense<A>,R> gs)
static <A,B,C>
F<Trampoline<A>,F<Trampoline<B>,Trampoline<C>>>liftM2(F<A,F<B,C>> f)
Promotes the given function of arity-2 to a function on Trampolines.<B> Trampoline<B>
map(F<A,B> f)
Maps the given function across the result of this trampoline.static <A,B>
F<F<A,B>,F<Trampoline<A>,Trampoline<B>>>map_()
static <A> F<A,Trampoline<A>>
pure()
static <A> Trampoline<A>
pure(A a)
Constructs a pure computation that results in the given value.abstract Either<P1<Trampoline<A>>,A>
resume()
Runs a single step of this computation.static <A> F<Trampoline<A>,Either<P1<Trampoline<A>>,A>>
resume_()
A
run()
Runs this computation all the way to the end, in constant stack.static <A> Trampoline<A>
suspend(F0<Trampoline<A>> a)
Suspends the given computation in a thunk.static <A> Trampoline<A>
suspend(P1<Trampoline<A>> a)
Suspends the given computation in a thunk.static <A> F<P1<Trampoline<A>>,Trampoline<A>>
suspend_()
<B,C>
Trampoline<C>zipWith(Trampoline<B> b, F2<A,B,C> f)
Combines two trampolines so they run cooperatively.
-
-
-
Method Detail
-
codense
private static <A,B> Trampoline.Codense<B> codense(Trampoline.Normal<A> a, F<A,Trampoline<B>> k)
-
pure
public static <A> F<A,Trampoline<A>> pure()
- Returns:
- The first-class version of
pure
.
-
pure
public static <A> Trampoline<A> pure(A a)
Constructs a pure computation that results in the given value.- Parameters:
a
- The value of the result.- Returns:
- A trampoline that results in the given value.
-
suspend
public static <A> Trampoline<A> suspend(F0<Trampoline<A>> a)
Suspends the given computation in a thunk.- Parameters:
a
- A trampoline suspended in a thunk.- Returns:
- A trampoline whose next step runs the given thunk.
-
suspend
public static <A> Trampoline<A> suspend(P1<Trampoline<A>> a)
Suspends the given computation in a thunk.- Parameters:
a
- A trampoline suspended in a thunk.- Returns:
- A trampoline whose next step runs the given thunk.
-
suspend_
public static <A> F<P1<Trampoline<A>>,Trampoline<A>> suspend_()
- Returns:
- The first-class version of
suspend
.
-
fold
protected abstract <R> R fold(F<Trampoline.Normal<A>,R> n, F<Trampoline.Codense<A>,R> gs)
-
bind
public abstract <B> Trampoline<B> bind(F<A,Trampoline<B>> f)
Binds the given continuation to the result of this trampoline.- Parameters:
f
- A function that constructs a trampoline from the result of this trampoline.- Returns:
- A new trampoline that runs this trampoline, then continues with the given function.
-
map
public final <B> Trampoline<B> map(F<A,B> f)
Maps the given function across the result of this trampoline.- Parameters:
f
- A function that gets applied to the result of this trampoline.- Returns:
- A new trampoline that runs this trampoline, then applies the given function to the result.
-
bind_
public static <A,B> F<F<A,Trampoline<B>>,F<Trampoline<A>,Trampoline<B>>> bind_()
- Returns:
- The first-class version of
bind
.
-
map_
public static <A,B> F<F<A,B>,F<Trampoline<A>,Trampoline<B>>> map_()
- Returns:
- The first-class version of
map
.
-
resume_
public static <A> F<Trampoline<A>,Either<P1<Trampoline<A>>,A>> resume_()
- Returns:
- The first-class version of
resume
.
-
resume
public abstract Either<P1<Trampoline<A>>,A> resume()
Runs a single step of this computation.- Returns:
- The next step of this compuation.
-
run
public final A run()
Runs this computation all the way to the end, in constant stack.- Returns:
- The end result of this computation.
-
apply
public final <B> Trampoline<B> apply(Trampoline<F<A,B>> lf)
Performs function application within a Trampoline (applicative functor pattern).- Parameters:
lf
- A Trampoline resulting in the function to apply.- Returns:
- A new Trampoline after applying the given function through this Trampoline.
-
bind
public final <B,C> Trampoline<C> bind(Trampoline<B> lb, F<A,F<B,C>> f)
Binds the given function across the result of this Trampoline and the given Trampoline.- Parameters:
lb
- A given Trampoline to bind the given function with.f
- The function to combine the results of this Trampoline and the given Trampoline.- Returns:
- A new Trampoline combining the results of the two trampolines with the given function.
-
liftM2
public static <A,B,C> F<Trampoline<A>,F<Trampoline<B>,Trampoline<C>>> liftM2(F<A,F<B,C>> f)
Promotes the given function of arity-2 to a function on Trampolines.- Parameters:
f
- The function to promote to a function on Trampolines.- Returns:
- The given function, promoted to operate on Trampolines.
-
zipWith
public final <B,C> Trampoline<C> zipWith(Trampoline<B> b, F2<A,B,C> f)
Combines two trampolines so they run cooperatively. The results are combined with the given function.- Parameters:
b
- Another trampoline to combine with this trampoline.f
- A function to combine the results of the two trampolines.- Returns:
- A new trampoline that runs this trampoline and the given trampoline simultaneously.
-
-