Skip to content

Section 2 Lesson 23: The Pipe Operator

Thiago Salles edited this page Jul 1, 2021 · 1 revision

The pipe operator |> passes the result of an expression as the first parameter of another expression.
It’s similar to the Unix | operator.

Its purpose is to highlight the data being transformed by a series of functions.

To see how it can make the code cleaner, have a look at this code:

Enum.sum(Enum.filter(Enum.map(1..100_000, &(&1 * 3)), odd?))

That's the same code rewritten using the |> operator:

1..100_000
|> Enum.map(&(&1 * 3))
|> Enum.filter(odd?)
|> Enum.sum()