Created
July 19, 2015 13:15
-
-
Save pofigizm/df730e5738f47597ecd3 to your computer and use it in GitHub Desktop.
Wrap recursion
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 sum(value) { | |
function line(fx) { | |
while (fx instanceof Function) { | |
fx = fx.call(null); | |
} | |
return fx; | |
} | |
function recursion(current, result) { | |
return function() { | |
if (current === 1) return result; | |
return recursion(current - 1, result + current); | |
}; | |
} | |
return line(recursion(value, 1)()); | |
} | |
console.log(sum(50000)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment