Skip to content

Instantly share code, notes, and snippets.

@dervalp
Created May 31, 2012 13:07
Show Gist options
  • Select an option

  • Save dervalp/2843309 to your computer and use it in GitHub Desktop.

Select an option

Save dervalp/2843309 to your computer and use it in GitHub Desktop.
Understanding Curry
var add = function(a, b, c) {
return a + b + c;
};
Function.prototype.method = function(name, func) {
this.prototype[name] = func;
return this;
};
Function.method('curry', function() {
//keep the value of curry inside a closure
var slice = Array.prototype.slice,
args = slice.apply(arguments),
that = this;
console.log("arguments in curry is - " + arguments);
console.log("args in curry is - " + args);
return function() {
//at that point the arguments is the arguments of the function (here add)
var finalarg = args.concat(slice.apply(arguments));
console.log("arguments in func is - " + arguments);
console.log("finalarg in func is - " + finalarg);
return that.apply(null, finalarg);
};
});
var add1 = add.curry(1,5);
console.log(add1(5));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment