← Back to lesson 09: Unpack &1 and the pipe

Unpack &1 and the pipe: full examples and further reading

The original lesson material is preserved here for reference. The main notebook focuses on this lesson's core question.

Write a full anonymous function before reading captures, Enum, and `|>`.

Run a full anonymous function, its &1 form, a captured String.trim/1, and one pipe that joins clean text.

Copy all of it: from the long form to the short form

All three parts give a function to another function.

Original full example
# Start with the full form: number is each value received
long_double = fn number -> number * 2 end
Enum.map([1, 2, 3], long_double)

# Now the short form: &1 is still each value received
Enum.map([1, 2, 3], &(&1 * 2))

# Capture the existing String.trim/1 function
["  peach", "plum  "] |> Enum.map(&String.trim/1)
|> Enum.join(", ")

The first two parts both return [2, 4, 6].

The last pipeline returns "peach, plum".

&1 only has meaning inside a short capture expression.

Steps for the full example

  1. Enum.map/2 takes two arguments: a collection and a function. That is why its name ends in /2.
  2. &(&1 * 2) means the same as fn number -> number * 2 end. Expand it when the short form feels unclear.
  3. &String.trim/1 does not call trim yet. It gives that function to Enum.map/2.
  4. The last line means Enum.join(previous_result, ", "). The pipe fills only the first argument automatically.

Original lesson notes

&1 is an argument placeholder, not a variable on its own. It only has meaning inside a capture expression that begins with &.

Run the full form fn number -> number * 2 end first. Then run &(&1 * 2) and compare the same result.

Functions in Enum process the items in a collection. The pipe |> places the result on its left into the first argument position of the function on its right.

Symbols and terms
&(&1 * 2)
Create an anonymous function. &1 is its first argument. The full form is fn number -> number * 2 end.
&String.trim/1
Capture the existing String.trim/1 function so other code can call it later.
|>
Send the result on the left into the first argument position of the function on the right.
capture operator
& can capture an existing function or create a short anonymous function.
Enum.map/2
Give each item in a collection to a function, then collect all the new return values.
pipe operator
|> puts the result on the left into the first argument position on the right.

Original full exercise

Add 1 with a full function and with &1. Then write a pipe that trims two strings with &String.trim/1 and joins them.

Exercise starter
# First: full function, then &1
full_result = Enum.map([2, 3, 4], fn number -> ____ end)
short_result = Enum.map([2, 3, 4], &(____))

# Next: capture String.trim/1 inside a pipe
clean_text =
  ["  red", "blue  "]
  |> Enum.map(____)
  |> Enum.join(____)

{full_result, short_result, clean_text}

The result should be {[3, 4, 5], [3, 4, 5], "red, blue"}.

Show hint

Use number + 1, &1 + 1, &String.trim/1, and the separator ", ".

Show original answer
Reference answer
# Full function, then its &1 short form
full_result = Enum.map([2, 3, 4], fn number -> number + 1 end)
short_result = Enum.map([2, 3, 4], &(&1 + 1))

# Capture trim/1 and write the pipe
clean_text =
  ["  red", "blue  "]
  |> Enum.map(&String.trim/1)
  |> Enum.join(", ")

{full_result, short_result, clean_text}
Does &String.trim/1 immediately remove spaces from a string?

No. It only captures the function. The function runs later, when Enum.map/2 gives each string to it.

Original lesson takeaways
Design trade-offs and language comparisons
DESIGN DECISION

Why this shape?

Why pass data through a series of small steps?

Choice

A pipeline turns nested calls into a top-to-bottom data journey. Each step should still be understandable and testable on its own.

Cost and boundary

A pipe is not a superior loop. If argument order is awkward or a step hides substantial I/O, an ordinary call can be clearer.

A FAMILIAR POINT OF VIEW

Coming from Java, Python, or JavaScript

Java

Familiar starting point
Stream chains or fluent method calls.
What BEAM changes
Elixir |> inserts the left value into the next call; Erlang usually nests calls or names intermediate values.
False friend
Enum is usually eager. Stream is lazy.

Python

Familiar starting point
Generators, comprehensions, and named intermediate values.
What BEAM changes
Captures and pipes compose small functions without mutating a list.
False friend
&String.trim/1 is a function value; String.trim/1 only names a function and arity.

JavaScript

Familiar starting point
map/filter chains, callbacks, and iterators.
What BEAM changes
Values are immutable by default; each step returns another result.
False friend
Do not read &1 as JavaScript's bitwise ampersand.
Original course references