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
trampoline returns that value itself. If the final
result should be function, it needs to wrapped (e.g. as a 1-elem
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)) // 
Generated using TypeDoc