OrdSet (ord_set v0.1.0)
Elixir wrapper around :ordsets
erlang module.
Acts like a normal MapSet
but adds in sorted order.
What this library does is forward the methods to the :ordsets
module but adds some Elixir flavour.
Like having the first argument be the set instead of the last,
have different function names to look like MapSet
OrdSet is just a list, so guards like in/2
, ==/2
just work™.
iex> 4 in OrdSet.new([1,2])
false
iex> 1 in OrdSet.new([1,2])
true
iex> OrdSet.new([1,2,1,2]) == OrdSet.new([1,2])
true
iex> OrdSet.new([1,2,1,2]) == OrdSet.new([3])
false
iex> [1,2,1,2,1,1] |> OrdSet.new() |> length()
2
Also Enum functions just work™.
iex> [2,5,4,5,2,1] |> OrdSet.new() |> Enum.sort()
OrdSet.new([1,2,4,5])
- ↩
You should ofcourse test this yourself
Link to this section Summary
Functions
Deletes the item from the set
Returns a set that is set_a without the members of set_b
Checks if set_a's members are all contained in set_b.
iex> OrdSet.equal?(OrdSet.new(1..3), OrdSet.new(1..3)) true iex> OrdSet.equal?(OrdSet.new(1..4), OrdSet.new(1..3)) false iex> OrdSet.equal?(OrdSet.new([1,2,3]), OrdSet.new([3,2,1])) true
Filters the set by returning only the elements from set
for which invoking fun
returns a truthy value.
Returns a set containing only members that set_a and set_b have in common.
iex> init = fn
...> set when OrdSet.is_empty(set) -> OrdSet.new([1])
...> set -> set
...> end
iex> OrdSet.new() |> init.()
OrdSet.new([1])
iex> [3,6] |> OrdSet.new() |> init.()
OrdSet.new([3,6])
Get the largest element in the set
iex> 2 in OrdSet.new(1..3) true iex> 1..3 |> OrdSet.new() |> OrdSet.member?(2) true iex> 1..3 |> OrdSet.new() |> OrdSet.member?(8) false
Get the smallest element in the set
Creates a new OrdSet
from Enumerable.t()
with given function
Puts the item to the end of the set
Returns a set by excluding the elements from set
for which invoking fun
returns a truthy value.
See :ordsets.size/1
.
Checks if set_a's members are all contained in set_b.
Converts OrdSet
to a list.
Returns a set containing all members of set_a and set_b.
Link to this section Types
@type t() :: list()
t(term)
@type t(term) :: [term]
Link to this section Functions
delete(set, item)
Deletes the item from the set
iex> [1,2,4,3] |> OrdSet.new() |> OrdSet.delete(2)
OrdSet.new([1,4,3])
difference(set_a, set_b)
Returns a set that is set_a without the members of set_b
iex> OrdSet.difference(OrdSet.new(1..4), OrdSet.new(3..6))
[1, 2]
disjoint?(set_a, set_b)
Checks if set_a's members are all contained in set_b.
iex> OrdSet.disjoint?(OrdSet.new([1, 2, 3]), OrdSet.new([4, 5, 6]))
true
iex> OrdSet.disjoint?(OrdSet.new([4, 5, 6]), OrdSet.new([2, 3, 4]))
false
equal?(set_a, set_b)
iex> OrdSet.equal?(OrdSet.new(1..3), OrdSet.new(1..3)) true iex> OrdSet.equal?(OrdSet.new(1..4), OrdSet.new(1..3)) false iex> OrdSet.equal?(OrdSet.new([1,2,3]), OrdSet.new([3,2,1])) true
filter(set, func)
@spec filter(t(), (term() -> as_boolean(term()))) :: t()
Filters the set by returning only the elements from set
for which invoking fun
returns a truthy value.
iex> MapSet.filter(MapSet.new(1..5), fn x -> x > 3 end)
MapSet.new([4, 5])
intersection(set_a, set_b)
Returns a set containing only members that set_a and set_b have in common.
iex> OrdSet.intersection(OrdSet.new(1..4), OrdSet.new(3..6))
[3, 4]
iex> init = fn
...> set when OrdSet.is_empty(set) -> OrdSet.new([1])
...> set -> set
...> end
iex> OrdSet.new() |> init.()
OrdSet.new([1])
iex> [3,6] |> OrdSet.new() |> init.()
OrdSet.new([3,6])
max(set)
Get the largest element in the set
iex> 0..5 |> OrdSet.new() |> OrdSet.max()
5
member?(set, item)
iex> 2 in OrdSet.new(1..3) true iex> 1..3 |> OrdSet.new() |> OrdSet.member?(2) true iex> 1..3 |> OrdSet.new() |> OrdSet.member?(8) false
min(set)
Get the smallest element in the set
iex> 0..5 |> OrdSet.new() |> OrdSet.min()
0
new()
@spec new() :: t()
Creates a empty OrdSet
new(list)
@spec new(Enumerable.t()) :: t()
Creates a new OrdSet
from Enumerable.t()
iex> OrdSet.new(0..5)
OrdSet.new([0,1,2,3,4,5])
iex> OrdSet.new(5..0//-1)
OrdSet.new([0,1,2,3,4,5])
new(enumerable, func)
@spec new(Enumerable.t(), (term() -> term())) :: t()
Creates a new OrdSet
from Enumerable.t()
with given function
iex> OrdSet.new([0,1,2,3,4,5], &rem(&1, 3))
OrdSet.new([0,1,2])
put(set, item)
Puts the item to the end of the set
iex> [1,2,3,4] |> OrdSet.new() |> OrdSet.put(5)
OrdSet.new([1,2,3,4,5])
iex> [1,2,3,4] |> OrdSet.new() |> OrdSet.put(3)
OrdSet.new([1,2,3,4])
iex> [1,2,4] |> OrdSet.new() |> OrdSet.put(3)
OrdSet.new([1,2,3,4])
reject(set, func)
@spec reject(t(), (term() -> as_boolean(term()))) :: t()
Returns a set by excluding the elements from set
for which invoking fun
returns a truthy value.
iex> OrdSet.reject(OrdSet.new(1..5), fn x -> rem(x, 2) != 0 end)
OrdSet.new([2, 4])
size(set)
@spec size(t()) :: pos_integer()
See :ordsets.size/1
.
subset?(set_a, set_b)
Checks if set_a's members are all contained in set_b.
iex> MapSet.subset?(MapSet.new([4]), MapSet.new([4, 5, 6]))
true
iex> OrdSet.subset?(OrdSet.new([4]), OrdSet.new([4, 5, 6]))
true
iex> MapSet.subset?(MapSet.new([4, 5, 6]), MapSet.new([4]))
false
iex> OrdSet.subset?(OrdSet.new([4, 5, 6]), OrdSet.new([4]))
false
to_list(set)
Converts OrdSet
to a list.
Ps: I don't think this does anything because a OrdSet
is already a list
union(set_a, set_b)
Returns a set containing all members of set_a and set_b.
iex> OrdSet.union(OrdSet.new(1..4), OrdSet.new(3..6))
[1,2,3,4,5,6]
iex> OrdSet.new(1..2) |> OrdSet.union(OrdSet.new(5..6)) |> OrdSet.union(OrdSet.new(1..3))
[1,2,3,5,6]