Skip to content

Instantly share code, notes, and snippets.

@mkulke
Created October 14, 2015 13:14
Show Gist options
  • Save mkulke/9bd719e42fb20d848127 to your computer and use it in GitHub Desktop.
Save mkulke/9bd719e42fb20d848127 to your computer and use it in GitHub Desktop.
node trampolining
function trampoline(f) {
while (f instanceof Function) {
f = f();
}
return f;
}
function factorial(n, acc) {
function recur(n, acc) {
acc = acc || 1;
if (n <= 1) return acc;
else return _.partial(recur, n - 1, n * acc);
}
return trampoline(_.partial(recur, n, acc));
}
console.log(factorial(100000));
function factorial(n, acc) {
acc = acc || 1;
if (n <= 1) return acc;
else return factorial(n - 1, n * acc);
}
console.log(factorial(100000));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment