Trampoline

public final class Trampoline<A> : TrampolineOf<A>

The Trampoline type helps us overcome stack safety issues of recursive calls by transforming them into loops.

  • Creates a Trampoline that does not need to recurse and provides the final result.

    Declaration

    Swift

    public static func done(_ value: A) -> Trampoline<A>

    Parameters

    value

    Result of the computation.

    Return Value

    A Trampoline that provides a value and stops recursing.

  • Creates a Trampoline that performs a computation and needs to recurse.

    Declaration

    Swift

    public static func `defer`(_ f: @escaping () -> Trampoline<A>) -> Trampoline<A>

    Parameters

    f

    Function describing the recursive step.

    Return Value

    A Trampoline that describes a recursive step.

  • Creates a Trampoline that performs a computation in a moment in the future.

    Declaration

    Swift

    public static func later(_ f: @escaping () -> A) -> Trampoline<A>

    Parameters

    f

    Function to compute the value wrapped in this Trampoline.

    Return Value

    A Trampoline that delays the obtention of a value and stops recursing.

  • Executes the computations described by this Trampoline by converting it into a loop.

    Declaration

    Swift

    public final func run() -> A

    Return Value

    Value resulting from the execution of the Trampoline.

  • Safe downcast.

    Declaration

    Swift

    public static func fix(_ fa: TrampolineOf<A>) -> Trampoline<A>

    Parameters

    fa

    Value in the higher-kind form.

    Return Value

    Value cast to Trampoline.