Skip to content

Instantly share code, notes, and snippets.

@verkholantsev
Created December 6, 2013 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save verkholantsev/7821520 to your computer and use it in GitHub Desktop.
Save verkholantsev/7821520 to your computer and use it in GitHub Desktop.
Trampoline implementation example
// http://jsfiddle.net/4yTPD/
debugger;
function sum (n) {
function nextStep (m, result) {
return function () {
if (m === 1) return result;
return nextStep(m-1, result + m);
}
}
return nextStep(n, 1)();
}
function trampoline (fn) {
while (_.isFunction(fn)) {
fn = fn.call(null);
}
return fn;
}
alert(trampoline(function () { return sum (30000) }));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment