Mizur v1.0.1 Mizur.Range
This module provides a minimalistic approach of Range between
typed_value
. A range is characterized by two values and a
direction. The two values must necessarily be different.
Summary
Functions
Checks if a range
is decreasing
Returns the first element of a range
Folds (reduces) the given range
from the left with a function.
Requires an accumulator
Folds (reduces) the given range
from the left with a function.
Requires an accumulator
Checks if a typed_value
is included in a range
Checks if a range
is increasing
Returns the last element of a range
Returns the the biggest typed_value
of a range
Returns the smallest typed_value
of a range
Builds a range between two typed_value
Checks if two ranges overlap
Reverses a range
Sorts a range
Tests if a range is a subrange of another range
Converts a range to a list of typed_value
Returns the type of a range
Types
This type represents a range of typed_value
.
Functions
Checks if a range
is decreasing.
iex> r = Mizur.Range.new(MizurTest.Distance.cm(10), MizurTest.Distance.cm(1))
...> Mizur.Range.decreasing?(r)
true
Returns the first element of a range.
iex> a = MizurTest.Distance.cm(1000)
...> b = MizurTest.Distance.cm(2)
...> c = Mizur.Range.new(a, b)
...> Mizur.Range.first(c)
MizurTest.Distance.cm(1000)
foldl(range, (Mizur.typed_value, any -> any), any, nil | Mizur.metric_type) :: any
Folds (reduces) the given range
from the left with a function.
Requires an accumulator.
iex> a = MizurTest.Distance.cm(1)
...> b = MizurTest.Distance.cm(10)
...> r = Mizur.Range.new(a, b)
...> Mizur.Range.foldl(r, fn(acc, x) -> [Mizur.unwrap(x) | acc] end, [])
Enum.map((10..1), fn(x) -> x * 1.0 end)
iex> a = MizurTest.Distance.m(0)
...> b = MizurTest.Distance.m(10_000)
...> r = Mizur.Range.new(a, b)
...> Mizur.Range.foldl(r, fn(acc, x) -> [Mizur.unwrap(x) | acc] end, [], MizurTest.Distance.km(1))
Enum.map((10..0), fn(x) -> x * 1000.0 end)
foldr(range, (Mizur.typed_value, any -> any), any, nil | Mizur.metric_type) :: any
Folds (reduces) the given range
from the left with a function.
Requires an accumulator.
iex> a = MizurTest.Distance.cm(1)
...> b = MizurTest.Distance.cm(10)
...> r = Mizur.Range.new(a, b)
...> Mizur.Range.foldr(r, fn(acc, x) -> [Mizur.unwrap(x) | acc] end, [])
Enum.map((1..10), fn(x) -> x * 1.0 end)
Checks if a typed_value
is included in a range.
iex> a = MizurTest.Distance.cm(1)
...> b = MizurTest.Distance.km(10)
...> r = Mizur.Range.new(a, b)
...> p = Mizur.Range.new(b, a)
...> x = MizurTest.Distance.m(1987)
...> {Mizur.Range.include?(x, in: r), Mizur.Range.include?(x, in: p)}
{true, true}
Checks if a range
is increasing.
iex> r = Mizur.Range.new(MizurTest.Distance.cm(1), MizurTest.Distance.cm(10))
...> Mizur.Range.increasing?(r)
true
Returns the last element of a range.
iex> a = MizurTest.Distance.cm(1000)
...> b = MizurTest.Distance.cm(2)
...> c = Mizur.Range.new(a, b)
...> Mizur.Range.last(c)
MizurTest.Distance.cm(2)
Returns the the biggest typed_value
of a range
.
iex> a = MizurTest.Distance.cm(1000)
...> b = MizurTest.Distance.cm(2)
...> c = Mizur.Range.new(a, b)
...> Mizur.Range.max(c)
MizurTest.Distance.cm(1000)
Returns the smallest typed_value
of a range
.
iex> a = MizurTest.Distance.cm(1000)
...> b = MizurTest.Distance.cm(2)
...> c = Mizur.Range.new(a, b)
...> Mizur.Range.min(c)
MizurTest.Distance.cm(2)
Builds a range between two typed_value
.
iex> Mizur.Range.new(MizurTest.Distance.cm(1), MizurTest.Distance.cm(10))
{MizurTest.Distance.cm(1), MizurTest.Distance.cm(10)}
iex> Mizur.Range.new(MizurTest.Distance.m(1), MizurTest.Distance.cm(10))
{MizurTest.Distance.m(1), MizurTest.Distance.m(10/100.0)}
Checks if two ranges overlap.
iex> a = MizurTest.Distance.m(1)
...> b = MizurTest.Distance.km(1)
...> x = MizurTest.Distance.m(20)
...> y = MizurTest.Distance.km(2)
...> r = Mizur.Range.new(a, b)
...> p = Mizur.Range.new(x, y)
...> Mizur.Range.overlap?(r, p)
true
iex> a = MizurTest.Distance.m(1)
...> b = MizurTest.Distance.km(1)
...> x = MizurTest.Distance.km(2)
...> y = MizurTest.Distance.km(20)
...> r = Mizur.Range.new(a, b)
...> p = Mizur.Range.new(x, y)
...> Mizur.Range.overlap?(r, p)
false
Reverses a range
:
iex> a = MizurTest.Distance.cm(1000)
...> b = MizurTest.Distance.cm(2)
...> c = Mizur.Range.new(a, b)
...> Mizur.Range.reverse(c)
Mizur.Range.new(MizurTest.Distance.cm(2), MizurTest.Distance.cm(1000))
Sorts a range.
iex> a = MizurTest.Distance.cm(1000)
...> b = MizurTest.Distance.cm(2)
...> c = Mizur.Range.new(a, b)
...> Mizur.Range.sort(c)
Mizur.Range.new(MizurTest.Distance.cm(2), MizurTest.Distance.cm(1000))
Tests if a range is a subrange of another range.
iex> a = MizurTest.Distance.m(1)
...> b = MizurTest.Distance.km(1)
...> r = Mizur.Range.new(a, b)
...> x = MizurTest.Distance.m(2)
...> y = MizurTest.Distance.m(900)
...> q = Mizur.Range.new(x, y)
...> Mizur.Range.subrange?(q, of: r)
true
Converts a range to a list of typed_value
.
iex> a = MizurTest.Distance.cm(1)
...> b = MizurTest.Distance.cm(10)
...> r = Mizur.Range.new(a, b)
...> Enum.map(Mizur.Range.to_list(r), &Mizur.unwrap/1)
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0]
Returns the type of a range
.
iex> a = MizurTest.Distance.cm(1000)
...> b = MizurTest.Distance.km(2)
...> c = Mizur.Range.new(a, b)
...> Mizur.Range.type_of(c)
MizurTest.Distance.cm()