3 ms·
It never occurred to me to try some currying in CoffeeScript, but man this looks expressive: f = (x) -> (y) -> x + y g = f 6
by chenglou 13y ago
It never occurred to me to try some currying in CoffeeScript, but man this looks expressive:
f = (x) -> (y) -> x + y
g = f 6
- tel 13y agoFor comparison, here's the same thing in Haskell f = \x -> \y -> x + y g = f 6 with the added bonus of automatic documentation ghci> :t f f :: Integer -> Integer -> Integer ghci> :t g g :: Integer -> Integer ghci> :t uncurry f uncurry f :: (Integer, Integer) -> Integer
- callmecosmas 13y agoYou don't actually need the second lambda since all Haskell functions take one argument, so you can just do f = \x y -> x + y and you'll have an implicit lambda before the y.
- alipang 13y agoAnd of course it has operator segmenting so you can just write f = (+)
- Peaker 13y agoOr simply: f x y = x + y
- tel 13y agoYes, but that has an obvious lack of syntactic parallelism, one especially important when talking about currying since you might be tempted to relate that to the coffee script (X, y) -> x+y It also has a subtly different performance characteristic in GHC, it turns out. Same semantics though.