Sponsored by AppSignal
Would you like to see your link here? Contact us
Notesclub

MapSet

7_mapsets.livemd

MapSet

import IEx.Helpers

Section

h(MapSet)

                                     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:

    iex> MapSet.new()
    MapSet.new([])

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

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

Elements can be inserted using MapSet.put/2:

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

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.new(["foo"])
    iex> map_set |> MapSet.put("foo") |> MapSet.put("foo")
    MapSet.new(["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.

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 Erlang's :sets
(https://www.erlang.org/doc/man/sets.html) (version 2). This means that they
share many properties, including logarithmic time complexity. Erlang :sets
(version 2) are implemented on top of maps, so see the documentation for Map
for more information on its execution time complexity.
empty = MapSet.new()
MapSet.new([])
eight = MapSet.new(1..8)
MapSet.new([1, 2, 3, 4, 5, 6, 7, 8])
MapSet.member?(eight, 8)
true
i(eight)
Term
  MapSet.new([1, 2, 3, 4, 5, 6, 7, 8])
Data type
  MapSet
Description
  This is a struct. Structs are maps with a __struct__ key.
Reference modules
  MapSet, Map
Implemented protocols
  Collectable, Enumerable, IEx.Info, Inspect
# match operator
%MapSet{} = eight
MapSet.new([1, 2, 3, 4, 5, 6, 7, 8])
%Date{} = eight
Map.keys(eight)
[:map, :__struct__]
Map.values(eight)
[%{1 => [], 2 => [], 3 => [], 4 => [], 5 => [], 6 => [], 7 => [], 8 => []}, MapSet]

Adding Values

MapSet.put(empty, :one)
MapSet.new([:one])
MapSet.put(empty, :one) |> MapSet.to_list()
[:one]

Set Operations

one_four = MapSet.new(1..4)
five_eight = MapSet.new(5..8)
two_six = MapSet.new(2..6)
MapSet.new([2, 3, 4, 5, 6])
MapSet.disjoint?(one_four, five_eight)
true
MapSet.intersection(one_four, five_eight)
MapSet.new([])
MapSet.intersection(one_four, two_six)
MapSet.new([2, 3, 4])
%{} |> Map.put(:one, []) |> Map.put(:one, [])
%{one: []}
MapSet.new() |> MapSet.put(:one) |> MapSet.put(:one)
MapSet.new([:one])