Created
December 6, 2013 10:17
-
-
Save verkholantsev/7821520 to your computer and use it in GitHub Desktop.
Trampoline implementation example
This file contains hidden or 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
// 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