Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us
Notesclub

MapSet

elixir_livebooks/map_set.livemd

MapSet

Functions that work on sets.

A set is a data structure that can contain unique elements of any kind, without any particular order. MapSet is the “go to” set data structure in Elixir.

A set can be constructed using MapSet.new/0:

MapSet.new()

Elements in a set don’t have to be of the same type and they can be populated from an enumerable using MapSet.new/1:

MapSet.new([1, :two, {"three"}])

Elements can be inserted using MapSet.put/2:

MapSet.new([2]) |> MapSet.put(4) |> MapSet.put(0)

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.

map_set = MapSet.new()
MapSet.put(map_set, "foo")
map_set |> MapSet.put("foo") |> MapSet.put("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:

match?(%MapSet{}, MapSet.new())

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.

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

Function delete/2

Deletes value from map_set.

Returns a new set which is a copy of map_set but without value.

Examples

map_set = MapSet.new([1, 2, 3])
MapSet.delete(map_set, 4)
MapSet.delete(map_set, 2)

Function difference/2

Returns a set that is map_set1 without the members of map_set2.

Examples

MapSet.difference(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))

Function disjoint?/2

Checks if map_set1 and map_set2 have no members in common.

Examples

MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([3, 4]))
MapSet.disjoint?(MapSet.new([1, 2]), MapSet.new([2, 3]))

Function equal?/2

Checks if two sets are equal.

The comparison between elements is done using ===/2, which a set with 1 is not equivalent to a set with 1.0.

Examples

MapSet.equal?(MapSet.new([1, 2]), MapSet.new([2, 1, 1]))
MapSet.equal?(MapSet.new([1, 2]), MapSet.new([3, 4]))
MapSet.equal?(MapSet.new([1]), MapSet.new([1.0]))

Function intersection/2

Returns a set containing only members that map_set1 and map_set2 have in common.

Examples

MapSet.intersection(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))
MapSet.intersection(MapSet.new([1, 2]), MapSet.new([3, 4]))

Function member?/2

Checks if map_set contains value.

Examples

MapSet.member?(MapSet.new([1, 2, 3]), 2)
MapSet.member?(MapSet.new([1, 2, 3]), 4)

Function new/0

Returns a new set.

Examples

MapSet.new()

Function new/1

Creates a set from an enumerable.

Examples

MapSet.new([:b, :a, 3])
MapSet.new([3, 3, 3, 2, 2, 1])

Function new/2

Creates a set from an enumerable via the transformation function.

Examples

MapSet.new([1, 2, 1], fn x -> 2 * x end)

Function put/2

Inserts value into map_set if map_set doesn’t already contain it.

Examples

MapSet.put(MapSet.new([1, 2, 3]), 3)
MapSet.put(MapSet.new([1, 2, 3]), 4)

Function size/1

Returns the number of elements in map_set.

Examples

MapSet.size(MapSet.new([1, 2, 3]))

Function subset?/2

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

MapSet.subset?(MapSet.new([1, 2]), MapSet.new([1, 2, 3]))
MapSet.subset?(MapSet.new([1, 2, 3]), MapSet.new([1, 2]))

Function to_list/1

Converts map_set to a list.

Examples

MapSet.to_list(MapSet.new([1, 2, 3]))

Function union/2

Returns a set containing all members of map_set1 and map_set2.

Examples

MapSet.union(MapSet.new([1, 2]), MapSet.new([2, 3, 4]))