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> set = MapSet.new
iex> MapSet.put(set, "foo")
#MapSet<["foo"]>
iex> 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.
Sets can also be constructed starting from other collection-type data structures: for example, see MapSet.new/1 or Enum.into/2.
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
Returns a set containing only members that set1 and set2 have in common
Checks if set contains value
Returns a new set
Creates a set from an enumerable
Creates a mapset from an enumerable via the transformation function
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
t() :: t(term)
t(value)
value() :: term
delete(t(val1), val2) :: t(val1) when val1: value, val2: value
Deletes value from set.
Returns a new set which is a copy of set but without value.
iex> set = MapSet.new([1, 2, 3])
iex> MapSet.delete(set, 4)
#MapSet<[1, 2, 3]>
iex> MapSet.delete(set, 2)
#MapSet<[1, 3]> difference(t(val1), t(val2)) :: t(val1) when val1: value, val2: value
Returns a set that is set1 without the members of set2.
iex> MapSet.difference(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
#MapSet<[1]> disjoint?(t, t) :: boolean
Checks if set1 and set2 have no members in common.
iex> MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([3, 4]))
true
iex> MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([2, 3]))
false equal?(t, t) :: boolean
Checks if two sets are equal.
The comparison between elements must be done using ===.
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 intersection(t(val), t(val)) :: t(val) when val: value
Returns a set containing only members that set1 and set2 have in common.
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<[]> member?(t, value) :: boolean
Checks if set contains value.
iex> MapSet.member?(MapSet.new([1, 2, 3]), 2)
true
iex> MapSet.member?(MapSet.new([1, 2, 3]), 4)
false new() :: t
Returns a new set.
iex> MapSet.new
#MapSet<[]> new(Enum.t) :: t
Creates a set from an enumerable.
iex> MapSet.new([:b, :a, 3])
#MapSet<[3, :a, :b]>
iex> MapSet.new([3, 3, 3, 2, 2, 1])
#MapSet<[1, 2, 3]> new(Enum.t, (term -> val)) :: t(val) when val: value
Creates a mapset from an enumerable via the transformation function.
iex> MapSet.new([1, 2, 1], fn x -> 2 * x end)
#MapSet<[2, 4]> put(t(val), new_val) :: t(val | new_val) when val: value, new_val: value
Inserts value into set if set doesn’t already contain it.
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]> size(t) :: non_neg_integer
Returns the number of elements in set.
iex> MapSet.size(MapSet.new([1, 2, 3]))
3 subset?(t, t) :: boolean
Checks if set1’s members are all contained in set2.
This function checks if set1 is a subset of set2.
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 to_list(t(val)) :: [val] when val: value
Converts set to a list.
iex> MapSet.to_list(MapSet.new([1, 2, 3]))
[1, 2, 3] union(t(val1), t(val2)) :: t(val1 | val2) when val1: value, val2: value
Returns a set containing all members of set1 and set2.
iex> MapSet.union(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
#MapSet<[1, 2, 3, 4]>
© 2012–2017 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/elixir/1.4.1/MapSet.html