Skip to content

Instantly share code, notes, and snippets.

@nakamura-to
Created March 29, 2012 12:27
Show Gist options
  • Save nakamura-to/2236938 to your computer and use it in GitHub Desktop.
Save nakamura-to/2236938 to your computer and use it in GitHub Desktop.
F#-like partial function application in JavaScript
function fun(f) {
return function curry() {
if (arguments.length < f.length) {
var args = Array.prototype.slice.call(arguments);
return function () {
return curry.apply(this, args.concat(Array.prototype.slice.call(arguments)));
}
} else {
return f.apply(this, arguments);
}
}
}
var add = fun(function (a, b, c) {
return a + b + c;
});
console.log(add(1, 2, 3)); // 6
console.log(add(1)(2, 3)); // 6
console.log(add(1)(2)(3)); // 6
console.log(add(1, 2)(3)); // 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment