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.

Source

Summary

delete(set, value)

Deletes value from set

difference(set1, set2)

Returns a set that is set1 without the members of set2

disjoint?(set1, set2)

Checks if set1 and set2 have no members in common

empty(set)

Returns an empty set of the same type as set

equal?(set1, set2)

Check if two sets are equal using ===

intersection(set1, set2)

Returns a set containing only members in common between set1 and set2

member?(set, value)

Checks if set contains value

put(set, value)

Inserts value into set if it does not already contain it

size(set)

Returns the number of elements in set

subset?(set1, set2)

Checks if set1's members are all contained in set2

to_list(set)

Converts set to a list

union(set1, set2)

Returns a set containing all members of set1 and set2

Types

value :: any

values :: [value]

t :: tuple

Functions

delete(set, value)

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]
Source
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(HashSet.new([1,2]), HashSet.new([2,3,4])) |> Enum.sort
[1]
Source
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?(HashSet.new([1, 2]), HashSet.new([3, 4]))
true
iex> Set.disjoint?(HashSet.new([1, 2]), HashSet.new([2, 3]))
false
Source
empty(set)

Specs:

  • empty(t) :: t

Returns an empty set of the same type as set.

Source
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?(HashSet.new([1, 2]), HashSet.new([2, 1, 1]))
true

iex> Set.equal?(HashSet.new([1, 2]), HashSet.new([3, 4]))
false
Source
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(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
[]
Source
member?(set, value)

Specs:

  • member?(t, value) :: boolean

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

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

Specs:

  • size(t) :: non_neg_integer

Returns the number of elements in set.

Examples

iex> Set.size(HashSet.new([1, 2, 3]))
3
Source
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?(HashSet.new([1, 2]), HashSet.new([1, 2, 3]))
true
iex> Set.subset?(HashSet.new([1, 2, 3]), HashSet.new([1, 2]))
false
Source
to_list(set)

Specs:

  • to_list(t) :: []

Converts set to a list.

Examples

iex> HashSet.to_list(HashSet.new([1, 2, 3])) |> Enum.sort
[1,2,3]
Source
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(HashSet.new([1,2]), HashSet.new([2,3,4])) |> Enum.sort
[1,2,3,4]
Source

Callbacks

delete(t, value)

Specs:

Source
difference(t, t)

Specs:

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

Specs:

  • disjoint?(t, t) :: boolean
Source
empty(t)

Specs:

  • empty(t) :: t
Source
equal?(t, t)

Specs:

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

Specs:

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

Specs:

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

Specs:

  • new :: t
Source
new(t)

Specs:

Source
new(t, list2)

Specs:

Source
put(t, value)

Specs:

Source
reduce(t, acc, reducer)

Specs:

Source
size(t)

Specs:

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

Specs:

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

Specs:

  • to_list(t) :: []
Source
union(t, t)

Specs:

  • union(t, t) :: t
Source