Skip to content

Instantly share code, notes, and snippets.

@thetristan
Last active December 17, 2015 11:29
Show Gist options
  • Save thetristan/5602550 to your computer and use it in GitHub Desktop.
Save thetristan/5602550 to your computer and use it in GitHub Desktop.
Currying w/ CoffeeScript
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