Skip to content

Instantly share code, notes, and snippets.

@mcsf
Created March 28, 2014 13:42
Show Gist options
  • Save mcsf/9833033 to your computer and use it in GitHub Desktop.
Save mcsf/9833033 to your computer and use it in GitHub Desktop.
Functional mixins for Underscore
_.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