10 comments

[ 5.7 ms ] story [ 43.1 ms ] thread
TIL Chris Okasaki works for West Point.
I started to use OCaml again and I noticed that they recently introduced the operator "|>". It lets you write things like "2 + 3 |> print_int" instead of "print_int (2 + 3)" or "let x = 2 + 3 in print_int x". It's particularly convenient for longer nested expressions.

Once you know that trick, it's tempting to use it everywhere and avoid parenthesis as much as possible. I'm not sure what the accepted practice is though as it doesn't always improve readability.

I also wonder why it took so long before they introduced it in the language. It's so simple and convenient. Did it take decades of people writing ML before someone thought of this?

The Left-Associative Reverse Function Application made me think of the "|>" too!, but I came across the |> in Elixir
My complaint with |> is that it makes code with nesting look quite different based on argument position. Argument position is not a very interesting property, so it doesn't seem right for it to be influencing the shape of code in that way.

My preference is to use parens and introduce single-shot variables if nesting gets too deep. This works the same way for all code.

Ocaml has had the @@ operator for a while, which is |> with it's arguements inversed, so you could write "print_int(2+3)" or "print_int @@ 2+3". Haskell is in a similar place, with the $ operator (equivalent to @@), but no |> in the standard library [0]. The main issue is that for a lot of people, there just isn't that much of a benefit to composing function left to right, as apposed to right to left; especially since the "simple" way of just using parentheses gives us right to left composition.

[0] If you want this, check out the flow package https://hackage.haskell.org/package/flow-1.0.8/docs/Flow.htm...

I believe the |> operator has been around for a while -- I've seen it used in some older Standard ML code, but just as a user-defined operator and not part of the basis library. F# included it in the core library for the language and it was fairly popular there for structuring heavily-nested expressions into a more-human-readable "pipeline". OCaml added it to the Core library fairly recently, as has (I believe) Elixir.
Okasaki really is brilliant