Explaining Elixir Pipes Through the Magic of Turduckens
One of the cooler features of Elixir (and other functional languages) is the
Pipe operator |>
. It certainly makes code easier to read, but it’s a bit
difficult to grep for the first time.
Let’s use the Turducken to explain it. For the uninitiated, A Turducken is a turkey stuffed with a duck which is itself stuffed in a chicken.
Code written sans pipe might look like:
You have to read this from the inside out. You:
- start at the output of
Turducken.stuff("chicken")
- figure out that goes into the
Turducken.stuff(_, "duck")
- and then THAT could into
Turducken.stuff(_, "turkey")
Better code might be to follow the transformation from inside to out (left to right):
Full code*
You can run this on the elixir playground
A more unixy example
In UNIX, we use pipe to take output from one operation into the other. Example: We’ll copy the contents of a file to the Mac clipboard.
The output of yolo goes into pbcopy. If this was a program (ruby), it could be:
In Elixir, you could write it as: