Skip to content

Instantly share code, notes, and snippets.

@pofigizm
Created July 19, 2015 13:15
Show Gist options
  • Save pofigizm/df730e5738f47597ecd3 to your computer and use it in GitHub Desktop.
Save pofigizm/df730e5738f47597ecd3 to your computer and use it in GitHub Desktop.
Wrap recursion
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