In a previous post I introduced the curry function. To recap, currying creates a new function with the first n arguments pre-assigned:-
var subtract = function(a,b) { return a - b; } var subtractFrom8 = subtract.curry(8); subtractFrom8(2); //6
Currying is an expressive and compact alternative to manually wrapping anonymous functions. I use it a lot. But sometimes its not enough – the problem is you can only pre-assign the first n arguments. What if we wanted to make a function and pre-assign the rightmost argument, or maybe the middle two? Enter partial:-
var subtract5 = subtract.partial(___,5); subtract5(13); //8;