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

t :: %{}
value :: any
values :: [value]

Functions

delete(set, value)

Specs

delete(t, value) :: t

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

Specs

difference(t, t) :: t

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

Specs

disjoint?(t, t) :: boolean

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

Specs

equal?(t, t) :: boolean

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

Specs

intersection(t, t) :: t

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
[]
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 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]
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.

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
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.

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]

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