Elixir v1.1.1 Set behaviour
This module specifies the Set behaviour expected to be
implemented by different representations of sets.
It also provides functions that redirect to the
underlying implementation, allowing a developer to work with
different Set implementations using a common API.
To create a new set, use the new function which each set implementation
defines:
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 the Enumerable and Collectable
protocols.
Matching
Sets are required to implement all equality checks 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
Checks if two sets are equal using ===
Returns a set containing only members that set1 and set2 have in common
Checks if set contains value
Inserts value into set if set doesn’t 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.
Returns a new set which is a copy of set but without value.
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.
Note that this function is polymorphic as it calculates the difference for
sets of the same type as well as of sets of different types. Each set
implementation also provides a difference function which only works with
sets of that 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.
Note that this function is polymorphic as it checks for disjoint sets of
any type. Each set implementation also provides a disjoint? function,
but that function 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
Checks if two sets are equal using ===.
Note that this function is polymorphic as it compares sets of
any type. Each set implementation also provides an equal?
function, but that function 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 that set1 and set2 have in common.
Note that this function is polymorphic as it calculates the intersection of
any type. Each set implementation also provides an intersection function,
but that function 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 set doesn’t 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.
This function checks if set1 is a subset of set2.
Note that this function is polymorphic as it checks the subset for
any type. Each set implementation also provides a subset? function,
but that function 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.
Note that this function is polymorphic as it calculates the union of sets of
any type. Each set implementation also provides a union function,
but that function 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]