Created
March 29, 2012 12:27
-
-
Save nakamura-to/2236938 to your computer and use it in GitHub Desktop.
F#-like partial function application in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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