trampoline-js v1.1.0
trampoline-js
Transforms recursion ⟹ loop
Usage
Install from npm
npm install trampoline-js
let { trampoline, done, more } = require('trampoline-js')
const times = (() => {
// optional accumulators & user input
const _times = (acc, n, s) =>
n <= 0
// on done (base condition)
? done(acc)
// continuation as thunks
// takes continuation function and params
: more(_times, `${acc}${s}`, n - 1, s)
// build trampoline by passing recursive function and optional initial objects
return trampoline(_times, '')
})()
times(5, '*')
// > '*****'API
function trampoline(genFun, args)
trampolinetakes generate/recursive function and optional arguments (optional). it returns a function of formfunction ([arguments]) {}.On invoking returned function,
genFunwill be called withargsfollowed by caller suppliedarguments.
genFunshould return eitherdone(a wrapper for base case) ormore(a wrapper function for continuation function). Otherwise, 'Invalid continuation' Error will be thrown.
function more(fun, args)
moreshould provide continuation function as first parameter and optional args.
function cont(fun, args)
Alias for
more.
function done(result)
doneis a base case wrapper which takes terminal object andresultwill be returned as output.
License
This plugin is licensed under the MIT license.
Copyright (c) 2016 Prince John Wesley