Created
January 18, 2016 14:27
-
-
Save mbaer3000/19b0b6378c636203ab4d to your computer and use it in GitHub Desktop.
Partial function application with arbitrary depth
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 add(x) { | |
// here's where it all starts, at the edge: add(x) | |
var sum = x; | |
// this is where the funk gets in: we're building a function that adds to | |
// the above sum and also provides a method to get to that sum, allowing | |
// to call the function arbitrarily often but also get to the sum, | |
// eventually. As in add(1)(2)(3)(4)(5).get() | |
var result = function(x) { | |
sum += x; | |
return result; | |
}; | |
result.get = function() { | |
return sum; | |
} | |
return result; | |
} | |
alert(add(1)(2)(3)(4).get() === 10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment