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 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.
  • Constructor Details

    • Trampoline

      public Trampoline()
  • Method Details

    • 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.