Skip to content

Instantly share code, notes, and snippets.

@karlwestin
Created April 13, 2012 11:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save karlwestin/2375986 to your computer and use it in GitHub Desktop.
Save karlwestin/2375986 to your computer and use it in GitHub Desktop.
Partial application javascript
var partial = function(func) {
return Function.prototype.bind.apply(func, arguments);
};
function add() {
var x = 0;
[].forEach.call(arguments, function(el) {
x += el;
});
return x;
};
var addnew = partial(add, 5, 6, 7);
console.log(addnew(1)); // 19
@karlwestin
Copy link
Author

What's really cool about this pattern is that it seems that you can apply partial to the returned function once again, so

    var addnew2 = partial(addnew, 2);
    console.log(addnew2(2)); // 22

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment