fj.control.Trampoline Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functionaljava Show documentation
Show all versions of functionaljava Show documentation
Functional Java is an open source library that supports closures for the Java programming language
package fj.control;
import fj.*;
import fj.data.Either;
import static fj.Function.curry;
import static fj.data.Either.left;
import static fj.data.Either.right;
/**
* A Trampoline is a potentially branching computation that can be stepped through and executed in constant stack.
* It represent suspendable coroutines with subroutine calls, reified as a data structure.
*/
public abstract class Trampoline {
// A Normal Trampoline is either done or suspended, and is allowed to be a subcomputation of a Codense.
// This is the pointed functor part of the Trampoline monad.
private static abstract class Normal extends Trampoline {
public abstract R foldNormal(final F pure, final F>, R> k);
public Trampoline bind(final F> f) {
return codense(this, f);
}
}
// A Codense Trampoline delimits a subcomputation and tracks its current continuation. Subcomputations are only
// allowed to be Normal, so all of the continuations accumulate on the right.
private static final class Codense extends Trampoline {
// The Normal subcomputation
private final Normal