Created
February 10, 2012 11:05
-
-
Save twfarland/1788801 to your computer and use it in GitHub Desktop.
Currying / Function composition in coffeescript
This file contains hidden or 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
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