Range
Defines a range.
A range represents a discrete number of values where the first and last values are integers.
Ranges can be either increasing (first <= last) or decreasing (first > last). Ranges are also always inclusive.
A Range is represented internally as a struct. However, the most common form of creating and matching on ranges is via the ../2
macro, auto-imported from Kernel
:
iex> range = 1..3 1..3 iex> first..last = range iex> first 1 iex> last 3
A Range implements the Enumerable protocol, which means all of the functions in the Enum module is available:
iex> range = 1..10 1..10 iex> Enum.reduce(range, 0, fn i, acc -> i * i + acc end) 385 iex> Enum.count(range) 10 iex> Enum.member?(range, 11) false iex> Enum.member?(range, 8) true
Summary
Types
Functions
- new(first, last)
-
Creates a new range
- range?(term)
-
Returns
true
if the giventerm
is a valid range
Types
t()
t() :: %Range{first: integer(), last: integer()}
t(first, last)
t(first, last) :: %Range{first: first, last: last}
Functions
new(first, last)
new(integer(), integer()) :: t()
Creates a new range.
range?(term)
range?(term()) :: boolean()
Returns true
if the given term
is a valid range.
Examples
iex> Range.range?(1..3) true iex> Range.range?(0) false
© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.4.5/Range.html