liveBook · Manning

Pipe operator

As explained in Elixir in Action, Third Edition

The Elixir pipe operator passes the result of an expression to the next function call as that call's first argument.

The pipe operator (|>) composes function calls by passing one expression’s result to the next call as its first argument. It replaces temporary variables and deeply nested calls with a left-to-right sequence that reads like a data transformation. section 1.2.2 section 2.3.2

Use the operator to make function flow visible:

  • Write `prev(arg1, arg2) |> next(arg3, arg4)` instead of `next(prev(arg1, arg2), arg3, arg4)`. section 2.3.2
  • Lay multiline pipelines out from top to bottom; in IEx, each complete pipeline line evaluates and displays its intermediate result. section 2.3.2
  • Understand the abstraction as compile-time transformation: the pipeline becomes the equivalent nested, or staircased, calls. section 1.2.2

Transform and print a value

The pipeline `-5 |> abs() |> Integer.to_string() |> IO.puts()` computes the absolute value, converts it to a string, and prints it. Its nested equivalent is `IO.puts(Integer.to_string(abs(-5)))`. section 2.3.2

def process_xml(model, xml) do
 model
 |> update(xml)
 |> process_changes()
 |> persist()
end

A small Elixir pipeline shows the idiom in a production-shaped transformation: update a model, process its changes, then persist it. section 1.2.2

How Elixir in Action, Third Edition covers it

How deep it goes
Explains syntax, first-argument placement, multiline use, IEx behavior, and compile-time expansion, but not broader macro implementation.
Examples
Uses model processing, nested Erlang-style calls, and an IEx chain converting -5 before printing it.
What it assumes
Assumes familiarity with function calls and functional programming while demonstrating composition through small Elixir expressions.
What you can do after
Apply it to chain Elixir function calls without temporary variables or manually nested expressions.
How it connects
Connects function composition with functional programming, macros, compile-time transformation, and IEx evaluation.

Also explained in

Compare how 6 books explain pipe operator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.