Created
April 13, 2012 11:15
-
-
Save karlwestin/2375986 to your computer and use it in GitHub Desktop.
Partial application javascript
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
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's really cool about this pattern is that it seems that you can apply partial to the returned function once again, so