Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created October 30, 2009 17:59
Show Gist options
  • Save phiggins42/222589 to your computer and use it in GitHub Desktop.
Save phiggins42/222589 to your computer and use it in GitHub Desktop.
(function(d){
d.compose = function(/* Function... */){
// summary: Returns the composition of a list of functions.
//
// description:
// Returns the composition of a list of functions, where each
// function consumes the return value of the function that follows.
//
// additionally, if one of the functions returns an array, the values
// of that array are passed to the next function in the composition
// as positional arguments.
//
// example:
// | var greet = function(name){ return "Hi: " + name; };
// | var exclaim = function(statement){ return statement + "!"; };
// | var welcome = dojo.compose(greet, exclaim);
// | welcome("Pete");
// | // Hi: Pete!
var list = d._toArray(arguments);
return function(){ // function
var a = arguments;
d.forEach(list, function(fn){
a = fn.apply(this, d.isArrayLike(a) ? a : [a]);
});
return a;
}
}
})(dojo);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment