Skip to content
The Pipe Operator
step 1/5

Reading — step 1 of 5

Learn

~1 min readFunctions and Modules

The pipe operator |> passes the result of the left expression as the FIRST argument of the right. It turns nested calls inside out:

# Without pipes — read inside out:
result = String.upcase(String.trim(input))

# With pipes — read top to bottom:
result =
    input
    |> String.trim()
    |> String.upcase()

This is one of Elixir's signature features. Pipelines compose data transformations with the same readability as a Unix shell pipeline.

[1, 2, 3, 4, 5]
|> Enum.map(&(&1 * &1))    # [1, 4, 9, 16, 25]
|> Enum.filter(&(&1 > 4))   # [9, 16, 25]
|> Enum.sum()               # 50
|> IO.puts()                 # prints 50

Elixir's standard library is designed around the convention that the "main" argument comes first, so piping is natural.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…