More JavaScript DSL
Another useful DSL technique is curry-ing. From WikiPedia: Currying is the technique of transforming a function that takes multiple arguments (or an n-tuple of arguments) in such a way that it can be called as a chain of functions each with a single argument.
As a cocktail of methods for currying I've seen, I use this:
Function.prototype.curry = function( fn ){
var slice = Array.prototype.slice;
return function(){
var args = slice.apply(arguments),
master = arguments.callee,
that = this;
return args.length >= fn.length ? fn.apply(that,args) : function(){
return master.apply( that, args.concat(slice.apply(arguments)) );
};
};
};
Basically it extends the Function prototype to allow you to write things like:
var sum = Function.curry(function( x, y ){
return x + y;
});
The above can then be used to write code as follows:
sum(5)(5); // 10
