Created
March 28, 2014 13:42
-
-
Save mcsf/9833033 to your computer and use it in GitHub Desktop.
Functional mixins for Underscore
This file contains 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
_.mixin({ | |
flipAll: function(f) { | |
return function() { | |
var args = Array.prototype.slice.call(arguments).reverse(); | |
return f.apply(this, args); | |
}; | |
}, | |
flip: function(f) { | |
return function() { | |
var args = Array.prototype.slice.call(arguments), | |
t = args[0]; | |
args[0] = args[1]; | |
args[1] = t; | |
return f.apply(this, args); | |
}; | |
} | |
}); | |
// divide = function(a, b) { return a / b } | |
// divide(6, 3) | |
// -> 2 | |
// _.flip(divide)(6, 3) | |
// -> 0.5 | |
// divideByTwo = _.partial(_.flip(divide), 2) | |
// divideByTwo(6) | |
// -> 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment