Elixir v1.9.2 MapSet View Source
Functions that work on sets.
MapSet
is the "go to" set data structure in Elixir. A set can be constructed
using MapSet.new/0
:
iex> MapSet.new()
#MapSet<[]>
A set can contain any kind of elements, and elements in a set don't have to be of the same type. By definition, sets can't contain duplicate elements: when inserting an element in a set where it's already present, the insertion is simply a no-op.
iex> map_set = MapSet.new()
iex> MapSet.put(map_set, "foo")
#MapSet<["foo"]>
iex> map_set |> MapSet.put("foo") |> MapSet.put("foo")
#MapSet<["foo"]>
A MapSet
is represented internally using the %MapSet{}
struct. This struct
can be used whenever there's a need to pattern match on something being a MapSet
:
iex> match?(%MapSet{}, MapSet.new())
true
Note that, however, the struct fields are private and must not be accessed directly; use the functions in this module to perform operations on sets.
MapSet
s can also be constructed starting from other collection-type data
structures: for example, see MapSet.new/1
or Enum.into/2
.
MapSet
is built on top of Map
, this means that they share many properties,
including logarithmic time complexity. See the documentation for Map
for more
information on its execution time complexity.
Link to this section Summary
Functions
Deletes value
from map_set
.
Returns a set that is map_set1
without the members of map_set2
.
Checks if map_set1
and map_set2
have no members in common.
Checks if two sets are equal.
Returns a set containing only members that map_set1
and map_set2
have in common.
Checks if map_set
contains value
.
Returns a new set.
Creates a set from an enumerable.
Creates a set from an enumerable via the transformation function.
Inserts value
into map_set
if map_set
doesn't already contain it.
Returns the number of elements in map_set
.
Checks if map_set1
's members are all contained in map_set2
.
Converts map_set
to a list.
Returns a set containing all members of map_set1
and map_set2
.
Link to this section Types
Link to this section Functions
Specs
Deletes value
from map_set
.
Returns a new set which is a copy of map_set
but without value
.
Examples
iex> map_set = MapSet.new([1, 2, 3])
iex> MapSet.delete(map_set, 4)
#MapSet<[1, 2, 3]>
iex> MapSet.delete(map_set, 2)
#MapSet<[1, 3]>
Specs
Returns a set that is map_set1
without the members of map_set2
.
Examples
iex> MapSet.difference(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
#MapSet<[1]>
Specs
Checks if map_set1
and map_set2
have no members in common.
Examples
iex> MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([3, 4]))
true
iex> MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([2, 3]))
false
Specs
Checks if two sets are equal.
The comparison between elements must be done using ===/2
.
Examples
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([2, 1, 1]))
true
iex> MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
false
Specs
Returns a set containing only members that map_set1
and map_set2
have in common.
Examples
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
#MapSet<[2]>
iex> MapSet.intersection(MapSet.new([1, 2]), MapSet.new([3, 4]))
#MapSet<[]>
Specs
Checks if map_set
contains value
.
Examples
iex> MapSet.member?(MapSet.new([1, 2, 3]), 2)
true
iex> MapSet.member?(MapSet.new([1, 2, 3]), 4)
false
Specs
new() :: t()
Returns a new set.
Examples
iex> MapSet.new()
#MapSet<[]>
Specs
Creates a set from an enumerable.
Examples
iex> MapSet.new([:b, :a, 3])
#MapSet<[3, :a, :b]>
iex> MapSet.new([3, 3, 3, 2, 2, 1])
#MapSet<[1, 2, 3]>
Specs
Creates a set from an enumerable via the transformation function.
Examples
iex> MapSet.new([1, 2, 1], fn x -> 2 * x end)
#MapSet<[2, 4]>
Specs
Inserts value
into map_set
if map_set
doesn't already contain it.
Examples
iex> MapSet.put(MapSet.new([1, 2, 3]), 3)
#MapSet<[1, 2, 3]>
iex> MapSet.put(MapSet.new([1, 2, 3]), 4)
#MapSet<[1, 2, 3, 4]>
Specs
size(t()) :: non_neg_integer()
Returns the number of elements in map_set
.
Examples
iex> MapSet.size(MapSet.new([1, 2, 3]))
3
Specs
Checks if map_set1
's members are all contained in map_set2
.
This function checks if map_set1
is a subset of map_set2
.
Examples
iex> MapSet.subset?(MapSet.new([1, 2]), MapSet.new([1, 2, 3]))
true
iex> MapSet.subset?(MapSet.new([1, 2, 3]), MapSet.new([1, 2]))
false
Specs
Converts map_set
to a list.
Examples
iex> MapSet.to_list(MapSet.new([1, 2, 3]))
[1, 2, 3]
Specs
Returns a set containing all members of map_set1
and map_set2
.
Examples
iex> MapSet.union(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
#MapSet<[1, 2, 3, 4]>