Last active
December 13, 2015 17:38
-
-
Save bigs/4949010 to your computer and use it in GitHub Desktop.
A cute currying function written in pure JavaScript for FUNZIES.
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 curry = function (f, args, binding) { | |
if (typeof f !== 'function' || | |
toString.call(args) !== '[object Array]') { | |
throw new Error('Invalid parameters'); | |
return; | |
} | |
if (typeof binding !== 'object') { | |
binding = this; | |
} | |
return function () { | |
var _args = args.concat(Array.prototype.slice.call(arguments)); | |
return f.apply(binding, _args); | |
}; | |
}; | |
// Example usage | |
var add = function(x, y, z) { | |
return x + y + z; | |
}; | |
var addTwo = curry(add, [2]); | |
console.log(addTwo(4, 6)); // 12 | |
console.log(addTwo(1, 3)); // 6 | |
var addTwoAndFour = curry(add, [2, 4]); | |
console.log(addTwoAndFour(6)); // 12 as well! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
made a final edit with some of your suggestions/hints