Created
June 15, 2019 06:47
-
-
Save vijayjangid/481cc8b7affed8f946eee059295e6cec to your computer and use it in GitHub Desktop.
infinite curried function for sum
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
function f (input, sum = 0) { | |
if(!input) { | |
return sum; | |
} | |
else { | |
return function(nextInput) { | |
sum += input; | |
return f(nextInput, sum); | |
} | |
} | |
} | |
// example | |
/* | |
f(); // 0 | |
f(1)(); // 1 | |
f(1)(2)(3)(); // 6 | |
f(3)(2)(5)(6)(); // 16 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment