The pipe operator in Elixir
In Elixir, the pipe operator |> takes the value on its left and passes it as the first argument of the call on its right. A chain of pipes reads top to bottom, in the order the data moves, instead of inside out through nested calls. It is only syntax: the calls are the same calls, and the value is the same value.
The value on the left
A pipeline starts with a value and has one call per line. Each stage takes the result of the stage above it, so the first line is the input and the last line is the output:
trim.exs
result = " hello " |> String.trim() |> String.upcase() IO.puts(result)
elixir trim.exs
HELLO
The same code written inside out is String.upcase(String.trim(" hello ")): the reader finds the input in the middle and follows the parentheses outward. The pipe puts the input first, which is why a long transformation is easier to read as a chain.
It lands in the first argument
A piped value always becomes the first argument of the next call, never the last. A call written with more arguments fills them after the piped one:
list.exs
result = [3, 1, 2] |> Enum.sort() |> Enum.sum() IO.puts(result)
elixir list.exs
6
Enum.sort([3, 1, 2]) returns [1, 2, 3], and Enum.sum([1, 2, 3]) returns 6. When a function takes its data last, the pipe cannot help, and the function is called with the arguments in their own order. That is why the standard library puts the collection first in almost every function.
A chain read top to bottom
A chain may hold any number of stages, and each one should do one job. This one reads a list of maps, joins the names and prints the result:
people.exs
people = [%{name: "Ada", age: 36}, %{name: "Grace", age: 85}] people |> Enum.map_join(", ", & &1.name) |> IO.puts()
elixir people.exs
Ada, Grace
Enum.map_join/3 maps and joins in one pass, which is why it replaces the pair of Enum.map/2 and Enum.join/2: a stage that does both jobs is faster and shorter than two stages. IO.puts/1 reads well at the end of a chain even though it returns :ok, because the chain ends there.
A lambda in a chain
then/2 is the stage that calls any function with the value, including one built for this chain only. It is how a lambda or a capture fits between two ordinary calls:
then.exs
result = "hello" |> then(&String.upcase/1) IO.puts(result)
elixir then.exs
HELLO
then/2 passes the piped value as the argument of the function it is given, so the capture works like any other stage. A longer body goes into a private function with a name, which reads better in a pipeline than a multi-line anonymous function in the middle of it.
The mistake: piping into a lambda
The pipe needs a call on its right, not a function value. Writing |> fn ... end hands the value to a lambda without calling it, and the expression stops with an ArgumentError that names the fix:
intofn.exs
"hello" |> fn text -> String.upcase(text) end
elixir intofn.exs
** (ArgumentError) cannot pipe "hello" into an anonymous function without calling the function; use Kernel.then/2 instead or define the anonymous function as a regular private function
(elixir 1.19.7) lib/macro.ex:299: Macro.pipe/3
(stdlib 6.2.2.5) lists.erl:2146: :lists.foldl/3
(elixir 1.19.7) expanding macro: Kernel.|>/2
intofn.exs:1: (file)
The message is long and exact: the value, the reason and the two fixes. The stack follows it. Use then/2 as the previous program does, or put the body in a named function and pipe into that. The same rule explains why a pipe into a bare function name without arguments is a syntax error: a stage is a call, and a call has parentheses when its only argument is the piped value.
The programs on this page were run with Elixir 1.19.7 on Erlang/OTP 27 (erts 15.2.7.14), on Linux x86_64 in a Docker container.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does the piped value go in the call on the right?
Answer
It becomes the first argument
The pipe fills the first argument slot, which is why collection-first functions pipe well.
-
What is
a |> f() |> g()equal to?Answer
g(f(a))
Each stage feeds the next, so the last call wraps the result of the earlier ones.
-
What does
then/2add to a pipeline?Answer
A stage that calls a function value with the piped value
then/2 applies the function it is given, which lets a lambda or capture join a chain.
-
Which call cannot be written as a pipe stage directly?
Answer
A bare anonymous function without a call
The right side of a pipe must be a call; a lambda is a value and needs then/2.
-
What does this program print?
IO.puts(" hi " |> String.trim() |> String.upcase())Answer
HI
The trim removes the spaces and the upcase capitalises the text.
-
What does this program print?
IO.puts([3, 1, 2] |> Enum.sort() |> Enum.sum())Answer
6
The list is sorted into [1, 2, 3] and the sum is 6.
-
What does this program print?
IO.puts(2 |> Kernel.+(3))Answer
5
The piped 2 becomes the first argument, so the call is Kernel.+(2, 3).
-
What does this program print?
IO.puts("a,b" |> String.split(",") |> Enum.join("-"))Answer
a-b
The split gives two strings and the join puts a dash between them.
-
What does this program do?
"hello" |> fn text -> String.upcase(text) endAnswer
An ArgumentError: it cannot pipe into an anonymous function without calling it
A pipe needs a call on the right; use then/2 or a named function instead.
-
What does this program print?
IO.puts("hello" |> then(&String.upcase/1))Answer
HELLO
then/2 calls the capture with the piped string, and the upper case is printed.
Article author: Arthur Isaev