liveBook · Manning

Interval

As explained in Elixir in Action, Third Edition

A range is an Elixir abstraction representing a sequence of values bounded by a start and an end.

A range represents a sequence of numbers between a start and an end. Elixir provides dedicated range syntax, membership testing with in, and enumeration through Enum. section 2.4.11

Ranges are useful because they:

  • Represent boundaries without storing every value. section 2.4.11
  • Keep a small, constant memory footprint, even for a million-number range. section 2.4.11
  • Work directly with Enum functions as enumerable values. section 2.4.11

Membership and enumeration

Define a range with 1..2, test membership with 2 in range, or pass 1..3 to Enum.each/2 to print its values. section 2.4.11

iex(4)> Enum.each(
         1..3,
         &IO.puts/1
       )

1
2
3

Inspect a runnable Enum.each example that iterates through a range. section 2.4.11

How Elixir in Action, Third Edition covers it

How deep it goes
Explains syntax, membership, enumeration, and constant-size map representation without detailing the Range implementation.
Examples
Uses 1..2 membership checks and Enum.each over 1..3.
What it assumes
Builds on numbers, maps, and enumerable operations as Elixir data abstractions.
What you can do after
Apply ranges through Elixir syntax, in, and Enum functions.
How it connects
Connects numeric membership with the in operator, iteration with Enum, and storage with maps.

Also explained in

Compare how 7 books explain interval →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.