Set behaviour
This module specifies the Set API expected to be implemented by different representations.
It also provides functions that redirect to the underlying Set, allowing a developer to work with different Set implementations using one API.
To create a new set, use the new functions defined
by each set type:
HashSet.new #=> creates an empty HashSet
For simplicity's sake, in the examples below every time
new is used, it implies one of the module-specific
calls like above.
Protocols
Sets are required to implement the Enumerable protocol,
allowing one to write:
Enum.each(set, fn k ->
IO.inspect k
end)
Match
Sets are required to implement all operations
using the match (===) operator. Any deviation from
this behaviour should be avoided and explicitly documented.
Summary
| delete(set, value) | Deletes |
| difference(set1, set2) | Returns a set that is |
| disjoint?(set1, set2) | Checks if |
| empty(set) | Returns an empty set of the same type as |
| equal?(set1, set2) | Check if two sets are equal using |
| intersection(set1, set2) | Returns a set containing only members in common between |
| member?(set, value) | Checks if |
| put(set, value) | Inserts |
| size(set) | Returns the number of elements in |
| subset?(set1, set2) | Checks if |
| to_list(set) | Converts |
| union(set1, set2) | Returns a set containing all members of |
Functions
Specs:
Deletes value from set.
Examples
iex> s = HashSet.new([1, 2, 3])
...> Set.delete(s, 4) |> Enum.sort
[1, 2, 3]
iex> s = HashSet.new([1, 2, 3])
...> Set.delete(s, 2) |> Enum.sort
[1, 3]
Specs:
Returns a set that is set1 without the members of set2.
Notice this function is polymorphic as it calculates the difference
for of any type. Each set implementation also provides a difference
function, but they can only work with sets of the same type.
Examples
iex> Set.difference(HashSet.new([1,2]), HashSet.new([2,3,4])) |> Enum.sort
[1]
Specs:
Checks if set1 and set2 have no members in common.
Notice this function is polymorphic as it checks for disjoint sets of
any type. Each set implementation also provides a disjoint? function,
but they can only work with sets of the same type.
Examples
iex> Set.disjoint?(HashSet.new([1, 2]), HashSet.new([3, 4]))
true
iex> Set.disjoint?(HashSet.new([1, 2]), HashSet.new([2, 3]))
false
Specs:
Check if two sets are equal using ===.
Notice this function is polymorphic as it compares sets of
any type. Each set implementation also provides an equal?
function, but they can only work with sets of the same type.
Examples
iex> Set.equal?(HashSet.new([1, 2]), HashSet.new([2, 1, 1]))
true
iex> Set.equal?(HashSet.new([1, 2]), HashSet.new([3, 4]))
false
Specs:
Returns a set containing only members in common between set1 and set2.
Notice this function is polymorphic as it calculates the intersection of
any type. Each set implementation also provides a intersection function,
but they can only work with sets of the same type.
Examples
iex> Set.intersection(HashSet.new([1,2]), HashSet.new([2,3,4])) |> Enum.sort
[2]
iex> Set.intersection(HashSet.new([1,2]), HashSet.new([3,4])) |> Enum.sort
[]
Specs:
Checks if set contains value.
Examples
iex> Set.member?(HashSet.new([1, 2, 3]), 2)
true
iex> Set.member?(HashSet.new([1, 2, 3]), 4)
false
Specs:
Inserts value into set if it does not already contain it.
Examples
iex> Set.put(HashSet.new([1, 2, 3]), 3) |> Enum.sort
[1, 2, 3]
iex> Set.put(HashSet.new([1, 2, 3]), 4) |> Enum.sort
[1, 2, 3, 4]
Specs:
- size(t) :: non_neg_integer
Returns the number of elements in set.
Examples
iex> Set.size(HashSet.new([1, 2, 3]))
3
Specs:
Checks if set1's members are all contained in set2.
Notice this function is polymorphic as it checks the subset for
any type. Each set implementation also provides a subset? function,
but they can only work with sets of the same type.
Examples
iex> Set.subset?(HashSet.new([1, 2]), HashSet.new([1, 2, 3]))
true
iex> Set.subset?(HashSet.new([1, 2, 3]), HashSet.new([1, 2]))
false
Specs:
- to_list(t) :: []
Converts set to a list.
Examples
iex> HashSet.to_list(HashSet.new([1, 2, 3])) |> Enum.sort
[1,2,3]
Specs:
Returns a set containing all members of set1 and set2.
Notice this function is polymorphic as it calculates the union of
any type. Each set implementation also provides a union function,
but they can only work with sets of the same type.
Examples
iex> Set.union(HashSet.new([1,2]), HashSet.new([2,3,4])) |> Enum.sort
[1,2,3,4]