Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@rhysbrettbowen
Created May 1, 2014 13:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhysbrettbowen/6ddfd6ffdd836066e2e7 to your computer and use it in GitHub Desktop.
Save rhysbrettbowen/6ddfd6ffdd836066e2e7 to your computer and use it in GitHub Desktop.
// add methods ending in 'f' to flip first two args
_.extend(_,_.reduce(_.keys(_),function(m,k){m[k+'f']=function(a,b){return _[k](b,a)};return m},{}));
// add a 'flip' property to functions (loses context if important):
Function.prototype.flip = function() {
var args = [].slice.apply(arguments);
var tmp = args[0]; args[0] = args[1]; args[1] = args[0];
return this.apply(this, args);
}
// usage: _.each.flip(myFn, [1,2,3])
// partial application with placeholder:
var partial = function(fn) {
var savedArgs = [].slice.call(arguments,1);
return function() {
var applyArgs = [].slice.call(arguments);
var args = [];
for (var i = 0; i < savedArgs.length + arguments.length; i++) {
args[i] = savedArgs[i] !== partial._ ? savedArgs[i] : applyArgs.shift();
}
return fn.apply(this, args.concat(applyArgs));
};
};
partial._={};
// usage:
var eachMyFn = patial(_.each, partial._, myFn);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment