Created
December 6, 2013 16:12
-
-
Save redbluish/7827361 to your computer and use it in GitHub Desktop.
curry.js
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
| ;(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