Created
June 30, 2014 04:53
-
-
Save finalprototype/eed4427fc9cadc880612 to your computer and use it in GitHub Desktop.
Expected argument length curry
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 add (a, b) { | |
| return a + b; | |
| } | |
| function multiply (a, b) { | |
| return a * b; | |
| } | |
| function applyFn (fn) { | |
| var parentArgs = Array.prototype.slice.call(arguments, 0); | |
| function wrapper () { | |
| var args = parentArgs.slice(1); | |
| args = args.concat(Array.prototype.slice.call(arguments, 0)); | |
| if (args.length >= 2) { | |
| return fn.apply(this, args); | |
| } | |
| return applyFn.apply(this, parentArgs.concat(args)); | |
| } | |
| return arguments.length-1 >= 2 ? wrapper() : wrapper; | |
| } | |
| var f = applyFn(multiply); | |
| console.log('3x5', f(3)(5)); | |
| console.log('2x8', f(2)(8)); | |
| f = applyFn(add); | |
| console.log('3+5', f(3)(5)); | |
| console.log('2+8', f(2)(8)); | |
| console.log('2+8', f(2)(8)(10)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment