concept range in category elixir

appears as: A rge, range, rge, rge, Rges
Elixir in Action

This is an excerpt from Manning's book Elixir in Action.

A range is an abstraction that allows you to represent a range of numbers. Elixir even provides a special syntax for defining ranges:

iex(1)> range = 1..2

You can ask whether a number falls in the range by using the in operator:

iex(2)> 2 in range
true

iex(3)> -1 in range
false

Ranges are enumerable, so functions from the Enum module know how to work with them. Earlier you met Enum.each/2, which iterates through an enumerable. The following example uses this function with a range to print the first three natural numbers:

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

1
2
3

It’s important to realize that a range isn’t a special type. Internally, it’s represented as a map that contains range boundaries. You shouldn’t rely on this knowledge, because the range representation is an implementation detail, but it’s good to be aware that the memory footprint of a range is very small, regardless of the size. A million-number range is still just a small map.