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:

Turducken.stuff(Turducken.stuff(Turducken.stuff("chicken"), "duck"), "turkey")

You have to read this from the inside out. You:

  1. start at the output of Turducken.stuff("chicken")
  2. figure out that goes into the Turducken.stuff(_, "duck")
  3. and then THAT could into Turducken.stuff(_, "turkey")

Better code might be to follow the transformation from inside to out (left to right):

"chicken" |> Turducken.stuff |> Turducken.stuff("duck") |> Turducken.stuff("turkey")

Full code*

defmodule Turducken do

  def stuff(animal) do

  def stuff(animal, into) do
    IO.inspect "You put the #{animal} into the #{into}"


#Turducken.stuff(Turducken.stuff(Turducken.stuff("chicken"), "duck"), "turkey")

"chicken" |> Turducken.stuff |> Turducken.stuff("duck") |> Turducken.stuff("turkey")

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.

cat yolo.md | pbcopy

The output of yolo goes into pbcopy. If this was a program (ruby), it could be:


In Elixir, you could write it as:

cat("yolo.md") |> pbcopy

# Or even

"yolo.md" |> cat |> pbcopy