Last active
August 29, 2015 14:01
-
-
Save carymrobbins/4007868fe1f4f8e23258 to your computer and use it in GitHub Desktop.
Function currying in JavaScript
This file contains hidden or 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
| var partial = function (f, partialArgs) { | |
| return function () { | |
| var args = []; | |
| Array.prototype.push.apply(args, partialArgs); | |
| Array.prototype.push.apply(args, arguments); | |
| return f.apply(this, args); | |
| }; | |
| }; | |
| var curry = function (f) { | |
| var arity = f.length; | |
| return function curried() { | |
| if (arguments.length >= arity) { | |
| return f.apply(this, arguments); | |
| } | |
| return partial(curried, arguments); | |
| }; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment