Elixir v1.1.0 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

t :: map
value :: any
values :: [value]

Functions

delete(set, value)

Specs

delete(t, value) :: t

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]
difference(set1, set2)

Specs

difference(t, t) :: t

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]
disjoint?(set1, set2)

Specs

disjoint?(t, t) :: boolean

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
equal?(set1, set2)

Specs

equal?(t, t) :: boolean

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
intersection(set1, set2)

Specs

intersection(t, t) :: t

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
[]
member?(set, value)

Specs

member?(t, value) :: boolean

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
put(set, value)

Specs

put(t, value) :: t

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]
size(set)

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
subset?(set1, set2)

Specs

subset?(t, t) :: boolean

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
to_list(set)

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]
union(set1, set2)

Specs

union(t, t) :: t

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]

Callbacks

delete(t, value)

Specs

delete(t, value) :: t
difference(t, t)

Specs

difference(t, t) :: t
disjoint?(t, t)

Specs

disjoint?(t, t) :: boolean
equal?(t, t)

Specs

equal?(t, t) :: boolean
intersection(t, t)

Specs

intersection(t, t) :: t
member?(t, value)

Specs

member?(t, value) :: boolean
new()

Specs

new :: t
put(t, value)

Specs

put(t, value) :: t
size(t)

Specs

size(t) :: non_neg_integer
subset?(t, t)

Specs

subset?(t, t) :: boolean
to_list(t)

Specs

to_list(t) :: list
union(t, t)

Specs

union(t, t) :: t