trampoline

Methods

(static) again(f, …a) → {Recur}

The again function should be used to wrap a function and its arguments in case the invocation needs to be suspended

Source:
Parameters:
Name Type Attributes Description
f function

The function which shall be suspended

a any <repeatable>

Arguments for next invocation

Returns:
Type:
Recur

A recursion wrapping data structure

Example
const {recur, again} = require('futils').trampoline;

const factorial = recur(function factLoop (n, m) {
    return n <= 1 ? m : again(factLoop, n - 1, n * m);
});

factorial(5, 1); // -> 120

(static) recur(f, …a) → {any}

The recur function for building recursive functions with trampolines.

Source:
Parameters:
Name Type Attributes Description
f function

The function which shall be suspended

a any <repeatable>

Arguments for next invocation

Returns:
Type:
any

The final value

Example
const {recur, again} = require('futils').trampoline;

const factorial = recur(function factLoop (n, m) {
    return n <= 1 ? m : again(factLoop, n - 1, n * m);
});

factorial(5, 1); // -> 120