Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Currying / Function composition in coffeescript
arr = Array::
arrSlice = arr.slice
curry = ->
args = arrSlice.call arguments
->
args2 = arrSlice.call arguments
args[0].apply @, args.slice(1).concat(args2)
sum = ->
args = arrSlice.call arguments
res = 0
res += n for n in args
res
alert sum 1,2,3
sumWith2 = curry sum, 2
alert sumWith2 1
compose = (f,g) ->
->
args = arrSlice.call arguments
f g.apply @, args
double = (n) -> n * 2
triple = (n) -> n * 3
doubleTriple = compose double, triple
alert doubleTriple 5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment