Options
All
  • Public
  • Public/Protected
  • All
Menu

External module "trampoline"

Index

Functions

Functions

Const trampoline

  • trampoline<T>(f: T | Fn0<T | Fn0<T>>): T
  • Takes a function returning either a no-arg function (thunk) or its already realized (non-function) result. Re-executes thunk for as long as it returns another function/thunk. Once a non-function result has been produced, trampoline returns that value itself. If the final result should be function, it needs to wrapped (e.g. as a 1-elem array).

    This function should be used for non-stack consuming recursion. I.e. a trampoline is a form of continuation passing style and only ever consumes max. 2 extra stack frames, independent from recursion depth.

    const countdown = (acc, x) =>
      x >= 0 ?
        () => (acc.push(x), countdown(acc, x-1)) :
        acc;
    
    trampoline(countdown([], 4))
    // [ 4, 3, 2, 1, 0 ]
    
    trampoline(countdown([], -1))
    // []

    Type parameters

    • T

    Parameters

    • f: T | Fn0<T | Fn0<T>>

    Returns T

Generated using TypeDoc