Skip to content

Instantly share code, notes, and snippets.

@Mparaiso
Forked from twfarland/currycompose.coffee
Created November 30, 2012 00:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mparaiso/4172782 to your computer and use it in GitHub Desktop.
Save Mparaiso/4172782 to your computer and use it in GitHub Desktop.
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