Partial: Curry’s flashy cousin
JavaScript Partials

		

		
			

	
	

	
		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;

I’ve augmented the Function prototype with an implementation of partial. I’m using a global variable, ___ (three underscores) to indicate an argument placeholder. If you’re nervous about name clashes in the global scope you may want to define a Partial object and set a property on it (e.g. Partial.___);
window.___ = {}; //argument placeholder

Function.prototype.partial = function() {
    if (arguments.length<1) {
        return this; //nothing to pre-assign - return the function as is
    }
    var __method = this;
    var args = arguments;
    return function() {
        //build up new arg list, for placeholders use current arg, otherwise copy original args
        var argIndex = 0, myArgs = [];
        for (var i = 0; i < args.length; i++) {
            myArgs[i] = window.___==args[i] ? arguments[argIndex++] : args[i];
        }
        return __method.apply(this, myArgs);
    }
}
The returned function iterates through the arguments we passed to partial, and looks for any placeholders. When it finds one it injects the next runtime argument in its place. With luck we now have the full set of arguments, a hybrid of pre-assigned and runtime args. (If there are fewer runtime arguments than placeholders, the jilted placeholders get replaced by undefined.)
The partial function offers a superset of the functionality provided by curry so theoretically we could drop curry from our library, However when you only need to replace the first n arguments, using curry means you don’t need specify placeholders:-
//curry using curry
var atLeast10 = Math.max.curry(10);

//curry using partial
var atLeast10 = Math.max.partial(10,___);
Now let’s give our new partial function a workout:-
Define a remove function:-
String.prototype.remove = String.prototype.replace.partial(___,'');

"12654I 2am2 13not12 3a45 3number3 453".remove(/\d/gi); //"I am not a number"
Get the cube root of anything…
var unCube = Math.pow.partial(___,1/3);

unCube(27); //3
unCube(15); //2.46621207433047
Call me later…
var later = timeOut.partial(___,1000);

later(alert.curry("here I am!"));//... ... "here I am!"
An intuitive variant on parseInt…
parseInt("035"); //29 ( literals starting with 0 assumed to be base 8 )

var toInt = parseInt.partial(___,10); //specifies base 10
toInt("035"); //35
Using jQuery’s array map function, a utility to increment each member by 1…
var incrementAll = jQuery.map.partial(___,function(x) {return x+1});

incrementAll([0,2,4,6,8]); //[1,3,5,7,9];
The partial function comes with an interesting side effect: The returned function will only use the first n arguments passed to it, where n is the number of placeholders that were passed to partial. Thus we can strictly limit the number of arguments visible to a function:-
var a = [1,2,3,4];
var b = [5,6,7,8];

//regular push adds all supplied arguments...
Array.prototype.push.apply(a,b); //a = [1,2,3,4,5,6,7,8];

//using partial we can create a pushFirst that will ignore surplus args....
var pushFirst = Array.prototype.push.partial(___);
pushFirst.apply(a,b); //a = [1,2,3,4,5]

			
										

	

			

		
		
	