Elixir v1.0.5 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
In the examples below, set_impl means a specific
Set implementation, for example HashSet.
Protocols
Sets are required to implement both Enumerable and Collectable
protocols.
Match
Sets are required to implement all operations using the match (===)
operator.
Summary
Functions
Deletes value from set
Returns a set that is set1 without the members of set2
Checks if set1 and set2 have no members in common
Check if two sets are equal using ===
Returns a set containing only members in common between set1 and set2
Checks if set contains value
Inserts value into set if it does not already contain it
Returns the number of elements in set
Checks if set1’s members are all contained in set2
Converts set to a list
Returns a set containing all members of set1 and set2
Types
Functions
Deletes value from set.
Examples
iex> s = Enum.into([1, 2, 3], set_impl.new)
iex> Set.delete(s, 4) |> Enum.sort
[1, 2, 3]
iex> s = Enum.into([1, 2, 3], set_impl.new)
iex> Set.delete(s, 2) |> Enum.sort
[1, 3]
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(Enum.into([1,2], set_impl.new), Enum.into([2,3,4], set_impl.new)) |> Enum.sort
[1]
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?(Enum.into([1, 2], set_impl.new), Enum.into([3, 4], set_impl.new))
true
iex> Set.disjoint?(Enum.into([1, 2], set_impl.new), Enum.into([2, 3], set_impl.new))
false
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?(Enum.into([1, 2], set_impl.new), Enum.into([2, 1, 1], set_impl.new))
true
iex> Set.equal?(Enum.into([1, 2], set_impl.new), Enum.into([3, 4], set_impl.new))
false
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(Enum.into([1,2], set_impl.new), Enum.into([2,3,4], set_impl.new)) |> Enum.sort
[2]
iex> Set.intersection(Enum.into([1,2], set_impl.new), Enum.into([3,4], set_impl.new)) |> Enum.sort
[]
Checks if set contains value.
Examples
iex> Set.member?(Enum.into([1, 2, 3], set_impl.new), 2)
true
iex> Set.member?(Enum.into([1, 2, 3], set_impl.new), 4)
false
Inserts value into set if it does not already contain it.
Examples
iex> Set.put(Enum.into([1, 2, 3], set_impl.new), 3) |> Enum.sort
[1, 2, 3]
iex> Set.put(Enum.into([1, 2, 3], set_impl.new), 4) |> Enum.sort
[1, 2, 3, 4]
Specs
size(t) :: non_neg_integer
Returns the number of elements in set.
Examples
iex> Set.size(Enum.into([1, 2, 3], set_impl.new))
3
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?(Enum.into([1, 2], set_impl.new), Enum.into([1, 2, 3], set_impl.new))
true
iex> Set.subset?(Enum.into([1, 2, 3], set_impl.new), Enum.into([1, 2], set_impl.new))
false
Specs
to_list(t) :: list
Converts set to a list.
Examples
iex> set_impl.to_list(Enum.into([1, 2, 3], set_impl.new)) |> Enum.sort
[1,2,3]
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(Enum.into([1,2], set_impl.new), Enum.into([2,3,4], set_impl.new)) |> Enum.sort
[1,2,3,4]