Trampoline

public class Trampoline<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.

  • Composes this trampoline with another one that depends on the output of this one.

    Declaration

    Swift

    public func flatMap<B>(_ f: @escaping (A) -> Trampoline<B>) -> Trampoline<B>

    Parameters

    f

    Function to compute a Trampoline based on the value of this one.

    Return Value

    A Trampoline describing the sequential application of both.

  • Transforms the eventual value provided by this Trampoline.

    Declaration

    Swift

    public func map<B>(_ f: @escaping (A) -> B) -> Trampoline<B>

    Parameters

    f

    Transforming function.

    Return Value

    A Trampoline that behaves as the original one but its result is transformed.