3 ms·
I've seen an interesting way to do method chaining in haskell: The '$' operator: Basically it's a low-priority right-associative binary operator that applies th
by voxcogitatio 17y ago
I've seen an interesting way to do method chaining in haskell: The '$' operator: Basically it's a low-priority right-associative binary operator that applies the function to the left to everything to the right.
Using the example from the article, one could do it as:
sort -n $ uniq -n $ sort $ awk '{ print $5}' $ grep tcp $ netstat -n
I.e essentially the same but in reverse order.
- eru 17y ago($) helps to avoid some parens. The (.) is more common to change functions, and closely related. ($) does not really compose functions, it is function application and right associative. Your example would rather read something close to sort -n . uniq -n . sort . awk '{ print $5}' . grep tcp . netstat -n (And `$ argument' at the end, if there was one.)
- samstokes 17y ago($) is actually the least interesting operator in the world :) But as you say, its parsing precedence makes it useful (because it lets you skip parens). What I like is how ($) generalises to (<$>) for applicative functors, so you can write succ $ 5 -- -> 6 succ <$> Nothing -- -> Nothing succ <$> Just 5 -- -> Just 6 succ <$> [1, 2, 3] -- -> [2, 3, 4] Who needs "for" loops? :)
- eru 17y agoI agree. Though I'd say id or const or even less interesting than ($).
- eru 17y ago(`to change functions' should have been `to chain functions'.)