Lately I was writing some semi-imperative code in OCaml and thinking to myself
“man, these ;
everywhere sure look ugly”. As I am quite a fan of the |>
operator, which threads the execution of multiple functions into a chain: every
function gets the result of the application of the previous function, so a |>
b |> c
corresponds to c (b a)
.
What I wanted was an operator like |>
which throws away the result of the
previous function and calls the next function in a normal way. So I named it
|-
, which coult be used like fun1 |- fun2
, which would evaluate to the
value of fun2
. We could even enforce that the value that fun1
is evaluated
to is to be ()
(unit), so we don’t throw away any important data.
So, let’s implement it:
1 2 |
|
Then it dawned on me: The operator I was looking for existed all along, it is
just ;
, I can replace all occurences of |-
by ;
!
This might look like a trivial epiphany, but it has indeed deepened my
understanding of the semicolon operator in the language. What I have previously
thought of as an purely syntactic element of the language turned out to be an
operator in disguise1 and rather unlike how for example Python uses ;
(to
concatenate multiple expressions into one expression).
-
Actually, this is not quite true, since
;
is indeed syntax, but can be thought as just syntactic sugar for an operator that drops the result of the evaluation of its first argument.↩