Skip to content

Instantly share code, notes, and snippets.

@mbaer3000
Created January 18, 2016 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbaer3000/19b0b6378c636203ab4d to your computer and use it in GitHub Desktop.
Save mbaer3000/19b0b6378c636203ab4d to your computer and use it in GitHub Desktop.
Partial function application with arbitrary depth
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