Created
May 1, 2014 13:29
-
-
Save rhysbrettbowen/6ddfd6ffdd836066e2e7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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