Created
October 14, 2015 13:14
-
-
Save mkulke/9bd719e42fb20d848127 to your computer and use it in GitHub Desktop.
node trampolining
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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