Skip to content

Instantly share code, notes, and snippets.

@twfarland
Created February 10, 2012 11:05
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save twfarland/1788801 to your computer and use it in GitHub Desktop.
Save twfarland/1788801 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