Operators and Style: & vs $, & >>> vs $ .?

This isn’t a discussion about how & >>> play together terribly, or how $ really should be infixl, but rather a discussion of the use of pipelines and composition operators.

In Haskell, we often use $ and . together, and this can actually create something accessible to traditional / imperative programmers, in that each use of . or $ indicates a step or a phase in the pipeline.

However, . and $ are based on the mathematical use, wherein the sequence of function application works from right to left, while & and >>> are quasi-imperative / traditional in that the staging occurs from left to right.

Which set of operators make for more readable code? Which set of operators make for more accessible code to new users?

Stylistically, should we continue to emphasize . and $, or is there a useful case for & and >>>?

1 Like

Which set of operators make for more readable code?

Depends who’s doing the reading. Seasoned Haskell programmers will find “reverse style” with ($) and (.) more comfortable; those from other languages like F# will enjoy “forward style” with (&)[^1] and (>>>).

It’s akin to asking whether dry white wine is more palatable than sweet white wine. Beauty is in the eye of the beholder and all that.

I work with a lot of new learners. Reverse style is a better teaching tool than forward style. It helps to make properties like substitution / referential transparency easier to understand, given that f $ x and f x are so visually similar. It also breaks people’s false intuitions around mutability; forward pipelines can get misread as a chain of mutations / method calls.

[^1]: Or, sometimes, |>, as has been adopted by IHP and a few others.

2 Likes

I think reverse style has the main benefit that, first, monadic style is forward style by default, and reverse style helps emphasize a distinction between monadic and non-monadic code.

I’m with the mainstream school that’s more familiar with reverse pipelines, but when you think of it beyond familiarity, reverse style is a mole, not a wart, given that you have to read the pipeline backwards.

1 Like