Skip to content

Instantly share code, notes, and snippets.

@bezhermoso
Created June 9, 2015 18:41
Show Gist options
  • Save bezhermoso/0a104d533bc404d7c494 to your computer and use it in GitHub Desktop.
Save bezhermoso/0a104d533bc404d7c494 to your computer and use it in GitHub Desktop.
Function currying in JavaScript with blank variable support.
Function.prototype.__ = {};
Function.prototype.curry = function () {
var fn = this,
args = Array.prototype.slice.call(arguments, 0);
while(args.length < fn.length) {
args.push(Function.prototype.__);
}
var supplied = args.filter(function(v) {
return Function.prototype.__ !== v;
});
if (supplied.length === fn.length) {
return fn.apply(this, supplied);
}
return function () {
var anonArgs = Array.prototype.slice.call(arguments, 0);
var newArgs = args.map(function(a) {
return (a === Function.prototype.__ && anonArgs.length > 0) ? anonArgs.shift() : a;
});
return Function.prototype.curry.apply(fn, newArgs);
};
};
/** Meanwhile in user-land... **/
var __ = Function.prototype.__;
var argsInspect = function (a, b, c, d) {
console.log([a, b, c, d]);
}
var a = argsInspect.curry(__, 2, __, 4);
a(1)(3); // [1, 2, 3, 4]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment