Ra

Ranges in Elixir

30 exercises

About Ranges

Ranges represent a sequence of one or many consecutive integers. They:

  • Are created using the .. operator and can be both ascending or descending.
  • Their default step is 1, but can be modified using the ..// operator (since Elixir 1.12).
  • Are inclusive of the first and last values.
  • Implement the Enumerable protocol.
  • Are represented internally as a struct, but can be pattern matched using ...
  • Can be used with integers written in the binary, octal, hexadecimal, and code point notation.
  • Can be turned into lists with functions such as Enum.to_list/1 or Enum.map/2.
Enum.to_list(9..1)
# => [9, 8, 7, 6, 5, 4, 3, 2, 1]

Enum.map(?A..?F, &<<&1>>)
["A", "B", "C", "D", "E", "F"]
Edit via GitHub The link opens in a new window or tab

Learn Ranges

Practicing is locked

Unlock 9 more exercises to practice Ranges