Created
May 23, 2016 18:41
-
-
Save danprince/a400d644d707f9279dcc88bc3aeee2cc to your computer and use it in GitHub Desktop.
Recursive Currying Into Unary Functions
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 curry(func) { | |
const arity = func.length; | |
function recurry(args=[]) { | |
if(args.length == arity) { | |
return func(...args); | |
} else { | |
return arg => recurry([...args, arg]); | |
} | |
} | |
return recurry(); | |
} |
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
const add = (a, b) => a + b; | |
const curriedAdd = curry(add); | |
curriedAdd(3)(5) // -> 8 | |
curriedAdd(3, 3)(5) // -> 8 (extra args are ignored) | |
const add3 = curriedAdd(3) // -> [Function] | |
add3(5) // -> 8 | |
add3(5, 6) // - 8 (extra args are ignored) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment