Skip to content

Instantly share code, notes, and snippets.

@redbluish
Created December 6, 2013 16:12
Show Gist options
  • Select an option

  • Save redbluish/7827361 to your computer and use it in GitHub Desktop.

Select an option

Save redbluish/7827361 to your computer and use it in GitHub Desktop.
curry.js
;(function (window, undefined) {
var _slice = [].slice;
function curry(fn) {
var arity = fn.length;
return (function given(argsSoFar) {
return function () {
var updatedArgsSoFar = argsSoFar.concat(_slice.call(arguments, 0));
return updatedArgsSoFar.length >= arity ?
fn.apply(this, updatedArgsSoFar) :
given(updatedArgsSoFar);
}
})([]);
}
window.curry = curry;
}).call(this, this);
function sumeOfFour(a, b, c, d) {
return a + b + c + d;
}
var curried = curry(sumeOfFour);
curried(1)(2)(3)(4); // => 10
curried(1, 2)(3, 4); // => 10
curried(1, 2, 3, 4); // => 10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment