An example of a more-complex partial application function (filling in the specified

from jquery ninja book

by dandoyon

HTML

<ul id="results"></ul>

CSS

#results li.pass { color: green; }
#results li.fail { color: red; }

JavaScript

function assert(value, desc) {
    var li = document.createElement("li");
    li.className = value ? "pass" : "fail";
    li.appendChild(document.createTextNode(desc));
    document.getElementById("results").appendChild(li);
}

// http://osteele.com/sources/javascript/functional/
Function.prototype.partial = function() {
    var fn = this,
        args = Array.prototype.slice.call(arguments);
    return function() {
        var arg = 0;
        for (var i = 0; i < args.length && arg < arguments.length; i++)
        if (args[i] === undefined) args[i] = arguments[arg++];
        return fn.apply(this, args);
    };
};

// only here to show difference 
Function.prototype.curry = function() {
    var fn = this,
        args = Array.prototype.slice.call(arguments);
    return function() {
        return fn.apply(this, args.concat(
        Array.prototype.slice.call(arguments)));
    };
};

String.prototype.csv = String.prototype.split.partial(/,\s*/);
var results = ("John, Resig, Boston").csv();
assert(results[1] == "Resig", "The text values were split properly");


/*
This implementation is fundamentally similar to the .curry() method, but has a couple
important differences. Notably, when called, the user can specify arguments that will be filled
in later by specifying undefined, for it. To accommodate this we have to increase the ability
of our arguments-merging technique. Effectively, we have to loop through the arguments
that are passed in and look for the appropriate gaps, filling in the missing pieces that were
specified.
*/