Skip to content

Instantly share code, notes, and snippets.

@haskellcamargo
Created June 8, 2015 14:30
Show Gist options
  • Save haskellcamargo/dab8b43298823fd24c78 to your computer and use it in GitHub Desktop.
Save haskellcamargo/dab8b43298823fd24c78 to your computer and use it in GitHub Desktop.
Currying
function curry$(f, bound){
var context,
_curry = function(args) {
return f.length > 1 ? function(){
var params = args ? args.concat() : [];
context = bound ? context || this : this;
return params.push.apply(params, arguments) <
f.length && arguments.length ?
_curry.call(context, params) : f.apply(context, params);
} : f;
};
return _curry();
}
// This makes any function curried:
var add = $curry(function(x, y) {
return x + y;
});
add(1); // => function(y) { return **x** + y; }
add(1, 2); // 3
add(1)(2); // 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment