View Source Guesswork.Constraint.Arithmetic.Range (Guesswork v0.8.0)
Indicates multiple possible integer values.
Negation
Because ranges are subsets of the denumerable integer set, it is possible to
represent Not <range>
as the complement of that range.
Enumerable
Because ranges can represent infinite sets you should be very careful when using
the Enum
module.
In general, Stream
is going to be a lot safer.
Summary
Functions
Adds all of the points in one range to all of the points of another.
Takes the complement of the set.
Return nil
for an empty range.
Inserts a single integer point into a range.
Inserts set of points into an existing range.
Checks if the range contains the integer.
Multiplies the range by an integer value.
Builds a new range that covers every possible integer.
Removes a single integer point from a range.
Removes a set of points from a range. Returns nil if the the set becomes empty.
Subtracts all of the points in one range to all of the points of another.
Types
Functions
Adds all of the points in one range to all of the points of another.
Examples
iex> r1 = Guesswork.Constraint.Arithmetic.Range.new(3, false)
...> r2 = Guesswork.Constraint.Arithmetic.Range.new(4, false)
...> Guesswork.Constraint.Arithmetic.Range.add(r1, r2)
%Guesswork.Constraint.Arithmetic.Range{values: [{7, 7}]}
iex> r1 = Guesswork.Constraint.Arithmetic.Range.new(3, false)
...> r2 = Guesswork.Constraint.Arithmetic.Range.new(4, 7, false)
...> Guesswork.Constraint.Arithmetic.Range.add(r1, r2)
%Guesswork.Constraint.Arithmetic.Range{values: [{7, 10}]}
iex> r1 = Guesswork.Constraint.Arithmetic.Range.new(3, :pos_inf, false)
...> r2 = Guesswork.Constraint.Arithmetic.Range.new(4, 7, false)
...> Guesswork.Constraint.Arithmetic.Range.add(r1, r2)
%Guesswork.Constraint.Arithmetic.Range{values: [{7, :pos_inf}]}
Takes the complement of the set.
Return nil
for an empty range.
Examples
iex> %Guesswork.Constraint.Arithmetic.Range{values: []}
...> |> Guesswork.Constraint.Arithmetic.Range.complement()
%Guesswork.Constraint.Arithmetic.Range{values: [{:neg_inf, :pos_inf}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(:neg_inf, :pos_inf, false)
...> |> Guesswork.Constraint.Arithmetic.Range.complement()
nil
iex> Guesswork.Constraint.Arithmetic.Range.new(:neg_inf, 4, false)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(7, :pos_inf)
...> |> Guesswork.Constraint.Arithmetic.Range.complement()
%Guesswork.Constraint.Arithmetic.Range{values: [{5, 6}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(-3, 4, false)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(7, 11)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(35, 98)
...> |> Guesswork.Constraint.Arithmetic.Range.complement()
%Guesswork.Constraint.Arithmetic.Range{
values: [{:neg_inf, -4}, {5, 6}, {12, 34}, {99, :pos_inf}]
}
Inserts a single integer point into a range.
Examples
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(4)
%Guesswork.Constraint.Arithmetic.Range{values: [{:neg_inf, 1}, {4, 4}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, false)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(1)
%Guesswork.Constraint.Arithmetic.Range{values: [{1, :pos_inf}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(-11)
%Guesswork.Constraint.Arithmetic.Range{values: [{:neg_inf, 1}]}
Inserts set of points into an existing range.
Examples
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(4, 2)
%Guesswork.Constraint.Arithmetic.Range{values: [{:neg_inf, 4}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, false)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(-1, 3)
%Guesswork.Constraint.Arithmetic.Range{values: [{-1, :pos_inf}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.insert(-11, :neg_inf)
%Guesswork.Constraint.Arithmetic.Range{values: [{:neg_inf, 1}]}
Checks if the range contains the integer.
Examples
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.member?(2)
false
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, false)
...> |> Guesswork.Constraint.Arithmetic.Range.member?(2)
true
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.member?(3)
false
Multiplies the range by an integer value.
@spec new() :: t() | nil
Builds a new range that covers every possible integer.
Removes a single integer point from a range.
Examples
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.remove(1)
%Guesswork.Constraint.Arithmetic.Range{values: [{:neg_inf, 0}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, false)
...> |> Guesswork.Constraint.Arithmetic.Range.remove(2)
%Guesswork.Constraint.Arithmetic.Range{values: [{3, :pos_inf}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.remove(-4)
%Guesswork.Constraint.Arithmetic.Range{values: [{:neg_inf, -5}, {-3, 1}]}
Removes a set of points from a range. Returns nil if the the set becomes empty.
Examples
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.remove(:neg_inf, 1)
nil
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, false)
...> |> Guesswork.Constraint.Arithmetic.Range.remove(:neg_inf, 4)
%Guesswork.Constraint.Arithmetic.Range{values: [{5, :pos_inf}]}
iex> Guesswork.Constraint.Arithmetic.Range.new(2, :pos_inf, true)
...> |> Guesswork.Constraint.Arithmetic.Range.remove(-8, -4)
%Guesswork.Constraint.Arithmetic.Range{values: [{:neg_inf, -9}, {-3, 1}]}
Subtracts all of the points in one range to all of the points of another.
Examples
iex> r1 = Guesswork.Constraint.Arithmetic.Range.new(3, false)
...> r2 = Guesswork.Constraint.Arithmetic.Range.new(4, false)
...> Guesswork.Constraint.Arithmetic.Range.subtract(r1, r2)
%Guesswork.Constraint.Arithmetic.Range{values: [{-1, -1}]}
iex> r1 = Guesswork.Constraint.Arithmetic.Range.new(3, false)
...> r2 = Guesswork.Constraint.Arithmetic.Range.new(4, 7, false)
...> Guesswork.Constraint.Arithmetic.Range.subtract(r2, r1)
%Guesswork.Constraint.Arithmetic.Range{values: [{1, 4}]}
iex> r1 = Guesswork.Constraint.Arithmetic.Range.new(3, :pos_inf, false)
...> r2 = Guesswork.Constraint.Arithmetic.Range.new(4, 7, false)
...> Guesswork.Constraint.Arithmetic.Range.subtract(r1, r2)
%Guesswork.Constraint.Arithmetic.Range{values: [{-4, :pos_inf}]}