Ranges
A range is a collection written as two bounds, 1..10, with a step that is usually 1. It takes a constant amount of memory whatever its size, counts its items without walking them, and enumerates in order, which makes it the natural source of a loop in Elixir. Under the dots it is a struct with three fields: first, last and step.
A range and its fields
The dots build the range, and the two bounds are read with the dot syntax of a struct. Enum.count/1 answers its size and Enum.to_list/1 turns it into a list when a program needs one:
basic.exs
range = 1..10 IO.inspect(range) IO.puts(range.first) IO.puts(range.last) IO.puts(Enum.count(range)) IO.inspect(Enum.to_list(range))
elixir basic.exs
1..10
1
10
10
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The inspector prints the range in its own notation rather than as a struct, which is why the first line reads 1..10. Every function of Enum and Stream takes a range, and so does a generator in a comprehension.
The step
A third number after two slashes sets the step: 1..10//2 is every second number from 1, and a negative step walks down. A range whose step points away from its last bound is empty, not an error:
step.exs
IO.inspect(1..10//2) IO.inspect(Enum.to_list(1..10//2)) IO.inspect(Enum.to_list(10..1//-1)) IO.inspect(Enum.to_list(5..1//-2)) IO.inspect(Enum.to_list(1..0//1))
elixir step.exs
1..10//2
[1, 3, 5, 7, 9]
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
[5, 3, 1]
[]
The last line is the empty range: the step is 1 and the last bound is below the first, so the range holds nothing and Enum.count/1 would answer 0. The step is always written when it is not 1, in both directions, which is what the mistake at the end of the page is about.
A range at work
A range is the shortest source for "do this n times", and a pipeline over it is the shortest form of a loop. A comprehension, a map and a sum need no list of numbers in between:
work.exs
IO.inspect(for n <- 1..5, do: n * n) IO.inspect(Enum.map(1..5, &(&1 * 2))) IO.puts(Enum.sum(1..100)) IO.puts(Enum.count(1..1_000_000))
elixir work.exs
[1, 4, 9, 16, 25]
[2, 4, 6, 8, 10]
5050
1000000
The last two lines are the point of the range: the sum of the first hundred numbers and the count of a million cost no memory, because the range is three numbers and both calls carry an accumulator. Enum.to_list/1 on the same range would build a million-item list, so the call to reach for is the one that aggregates.
The count that is free
Enum.count/1 of a range does not walk it: the protocol reads the bounds and the step and computes the size in one step. The same holds for Enum.member?/2, which compares the value with the bounds rather than scanning. A range is therefore the one collection whose length is constant, and the reason a large loop over 1..n allocates nothing.
The mistake: the step a descending range hides
A range whose last bound is below its first has a default step of -1, so 5..1 counts down. Relying on that default is deprecated, and the compiler says so, with the exact spelling it wants. The program below builds the list and prints nothing of its own, so the whole output block is the warning of the compiler:
mistake.exs
_ = Enum.to_list(5..1)
elixir mistake.exs
warning: 5..1 has a default step of -1, please write 5..1//-1 instead
mistake.exs:1: (file)
The program still runs and still produces the descending list that the section above shows, which is why the warning is easy to miss. The fix is the explicit form, 5..1//-1, which reads the same in every version and states the direction instead of leaving it to a default. A range that the program means to be empty is written 5..1//1, and the two forms differ in one character.
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.
-
How much memory does the range
1..1_000_000take?Answer
A constant amount, three numbers
A range is a struct of first, last and step, and its items are computed as they are asked for.
-
What does a step of -1 in a range do?
Answer
It counts down from the first bound to the last
The step is added to each value to get the next one, so a negative step walks down.
-
What is
Enum.to_list(1..0//1)?Answer
[]
The step points away from the last bound, so the range holds nothing.
-
Why is counting a range cheaper than counting a list?
Answer
Because Enum.count reads the bounds and computes the size
The Enumerable implementation of a range computes the count from first, last and step.
-
What does this program print?
IO.puts(Enum.count(1..10))Answer
10
The range includes both bounds, so 1 to 10 holds ten numbers.
-
What does this program print?
IO.inspect(Enum.to_list(1..10//3))Answer
[1, 4, 7, 10]
The step is 3 from 1, so 1, 4, 7 and 10 are in the range; 13 is past the last bound.
-
What does this program print?
IO.inspect(Enum.sum(1..4))Answer
10
The sum of the four numbers is 1 + 2 + 3 + 4.
-
What does this program print?
IO.inspect(5..1//-2 |> Enum.to_list())Answer
[5, 3, 1]
The step walks down by two from 5, so 5, 3 and 1 are the members.
-
What does this program print?
IO.inspect(Enum.to_list(2..8//2))Answer
[2, 4, 6, 8]
The step is 2 from the first bound, so every second number is a member.
-
Which spelling states the direction of a descending range?
Answer
5..1//-1
The explicit negative step is the form the compiler asks for.
Article author: Arthur Isaev