Chuyển đến nội dung

Bài 17 · Enumerables and Data Processing

Comprehensions with for

Learn Elixir for comprehensions: generators, filters, multiple generators, pattern matching, :into, :reduce, :uniq and bitstring generators, with clear examples.

Elixir's for is not a loop in the C or JavaScript sense. It is a comprehension: an expression that takes one or more collections, optionally filters and combines their elements, and builds a new collection from the results. If you like mathematical set notation, for x <- list, x > 2, do: x * x reads as "the squares of the elements of list that are greater than 2".

A comprehension can do the work of Enum.map, Enum.filter and nested loops in one readable expression. It also has features Enum lacks, such as several generators at once, pattern-matching generators and bitstring input. You should know the basics of the Enum module first.

Elixir cannot run on this site, so the Elixir code is read-only. The JavaScript TryIts mimic each result with array methods and loops.

Generators and the basic form

A generator has the form pattern <- enumerable. The comprehension runs its body once per element and collects the results in a list:

ELIXIR
for n <- [1, 2, 3], do: n * n
# [1, 4, 9]

for n <- 1..5, do: n * 2
# [2, 4, 6, 8, 10]

When the body is longer, use a do ... end block:

ELIXIR
for name <- ["ana", "ben"] do
  String.upcase(name) <> "!"
end
# ["ANA!", "BEN!"]

Results are always collected into a list by default, whatever the source was.

Parts of a comprehension

Generator pattern <- collection pulls elements.
Filter A condition that skips elements.
Body The expression that builds each result.
Options :into, :reduce and :uniq shape the output.

Result is a list unless you say otherwise.

1 / 2

Filters

After the generators you can add filters: any expression that returns a truthy or falsy value. Elements for which a filter is falsy are skipped.

ELIXIR
for n <- 1..10, rem(n, 2) == 0, do: n * n
# [4, 16, 36, 64, 100]

for n <- 1..20, rem(n, 3) == 0, rem(n, 2) == 1, do: n
# [3, 9, 15]

All filters must pass. A filter can be any expression, including a call to your own function, because it is not restricted like a guard.

JS Squares of even numbers

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Several generators

With more than one generator you get every combination, like nested loops. The last generator varies fastest:

ELIXIR
for rank <- ["A", "K"], suit <- [:hearts, :spades] do
  {rank, suit}
end
# [{"A", :hearts}, {"A", :spades}, {"K", :hearts}, {"K", :spades}]

Later generators and filters can use variables bound by earlier ones, so you can express triangular loops:

ELIXIR
for a <- 1..4, b <- a..4, a + b == 5, do: {a, b}
# [{1, 4}, {2, 3}]

This replaces a nested Enum.flat_map and Enum.filter, and it is often the most readable way to write cross products.

JS Cross product and a dependent generator

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Patterns in generators

The left side of <- is a pattern. Elements that do not match are silently skipped, which gives you filtering for free:

ELIXIR
results = [{:ok, 1}, {:error, :timeout}, {:ok, 2}, {:ok, 3}]

for {:ok, value} <- results, do: value
# [1, 2, 3]

users = [%{name: "ana", admin: true}, %{name: "ben", admin: false}]
for %{name: name, admin: true} <- users, do: name
# ["ana"]

Compare this with Enum.map, which would raise a FunctionClauseError on the first non-matching element. Use this behavior deliberately; if a skipped element would be a bug, use Enum.map so the crash shows it.

Changing the result with :into

By default you get a list. The :into option collects into any collectable: a map, a MapSet, a string, a file stream.

ELIXIR
for {k, v} <- %{a: 1, b: 2}, into: %{}, do: {k, v * 10}
# %{a: 10, b: 20}

for word <- ["Elixir", "Rust"], into: %{}, do: {word, String.length(word)}
# %{"Elixir" => 6, "Rust" => 4}

for c <- ~c"hello", c != ?l, into: "", do: <<c>>
# "heo"

for n <- [1, 2, 2, 3, 3, 3], into: MapSet.new(), do: n
# MapSet.new([1, 2, 3])

This is the idiomatic way to transform a map and get a map back. Add uniq: true to drop duplicate results: for n <- [1, 1, 2], uniq: true, do: n gives [1, 2].

JS Collecting into maps and sets

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Folding with :reduce

The :reduce option turns a comprehension into a fold. You give an initial accumulator, and the do block contains pattern -> new_accumulator clauses:

ELIXIR
for n <- 1..5, reduce: 0 do
  acc -> acc + n
end
# 15

for word <- ["a", "b", "a", "c", "a"], reduce: %{} do
  counts -> Map.update(counts, word, 1, &(&1 + 1))
end
# %{"a" => 3, "b" => 1, "c" => 1}

With :reduce, the body must return the next accumulator in every clause; it does not collect results into a list. The advantage over Enum.reduce/3 is that you can use several generators and filters in one expression. It is a good fit when you need to count or total over a cross product.

Bitstring generators

Strings are binaries, and binaries are sequences of bytes or bits. A bitstring generator uses <<pattern <- binary>> to walk a binary in chunks. By default each chunk is one byte, which for ASCII text is one character:

ELIXIR
for <<c <- "hello">>, do: c
# [104, 101, 108, 108, 111]

for <<c <- "hello">>, into: "", do: <<c + 1>>
# "ifmmp"

for <<r::8, g::8, b::8 <- <<255, 128, 0, 10, 20, 30>>>>, do: {r, g, b}
# [{255, 128, 0}, {10, 20, 30}]

The last example reads three bytes at a time, which is a natural way to parse binary data such as pixels. Because a byte is not always a whole character (UTF-8 can use up to four bytes per character), use String.graphemes/1 or <<c::utf8 <- text>> when the text may contain non-ASCII letters:

ELIXIR
for <<c::utf8 <- "héllo">>, do: c
# [104, 233, 108, 108, 111]
JS Reading bytes in groups of three

Press Run (or Ctrl + Enter) to see the console output here.

No output.

for or Enum?

For a simple map or filter over one list, Enum.map and Enum.filter in a pipeline are just as clear and chain better. Prefer for when you have several generators, want to filter by pattern, need :into or :reduce in one step, or are reading a binary. Remember that for is eager (it builds the whole result) and that variables bound inside it do not leak out; like everything else in Elixir, it never changes the data it reads. For lazy work, see Lazy Processing with Stream.

Check your understanding

Quick check · 5 questions

of 5 answered You scored / 5 · perfect!

  1. 1. What does for n <- [1, 2, 3], n > 1, do: n * 10 return?

    The filter n > 1 skips 1, and the body multiplies the rest by 10, collecting into a list by default.
  2. 2. What happens to elements that do not match the pattern on the left of <-?

    Pattern-matching generators filter: only elements that match are processed.
  3. 3. How do you get a map out of a comprehension?

    The :into option accepts any collectable, including a map, and each body result must be a {key, value} tuple.
  4. 4. In for a <- [1, 2], b <- [:x, :y], do: {a, b}, which generator changes fastest?

    Like nested loops, the last generator is the innermost, so the result is [{1, :x}, {1, :y}, {2, :x}, {2, :y}].
  5. 5. What does the :reduce option change?

    With reduce: initial, you write acc clauses that return the new accumulator, which becomes the final value.

Summary

  • A comprehension builds a collection from generators (pattern <- enumerable), filters and a body.
  • Several generators produce all combinations; later ones may depend on earlier variables.
  • Generator patterns skip non-matching elements.
  • into: chooses the result type (map, MapSet, string); uniq: true drops duplicates; reduce: turns the comprehension into a fold.
  • Bitstring generators (<<c <- binary>>) walk bytes or bit groups; use ::utf8 for multi-byte characters.
  • for is eager. Use Enum for simple single-list pipelines and Stream for lazy work.

Next: Lazy Processing with Stream.

Theo dõi tiến độ của bạn

Tạo tài khoản miễn phí để đánh dấu bài đã học và tiếp tục từ chỗ bạn dừng lại trên mọi thiết bị.