Last active
December 17, 2015 11:29
-
-
Save thetristan/5602550 to your computer and use it in GitHub Desktop.
Currying w/ CoffeeScript
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
partial = (fn, args...) -> | |
(innerArgs...) -> | |
args = args.concat(innerArgs) | |
fn(args...) | |
curry = (fn, arity = fn.length) -> | |
(args...) -> | |
innerFn = (arity) -> | |
if arity > 0 | |
curry partial(fn, args...), arity | |
else | |
fn(args...) | |
innerFn(arity - args.length) | |
regularFn = (a, b, c) -> a + b + c | |
regularFn(1, 2, 3) | |
# -> 6 | |
curriedFn = curry(regularFn) | |
curriedFn(1)(2)(3) | |
curriedFn(1, 2)(3) | |
curriedFn(1)(2, 3) | |
curriedFn(1, 2, 3) | |
# All invocations return -> 6 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment