fj.control.Trampoline Maven / Gradle / Ivy
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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy