liveBook · Manning

MapSet

As explained in Elixir in Action, Third Edition

MapSet is an Elixir collection implementing a set of unique values of any type.

MapSet is an Elixir collection for unique values of any type. It provides a higher-level set abstraction on top of built-in data structures and is implemented in pure Elixir. section 2.4.11 section 4.1

Use the module functions as the abstraction boundary:

  • Create a set with MapSet.new/0 or MapSet.new/1. section 4.1
  • Check membership with MapSet.member?/2. section 2.4.11
  • Add values with MapSet.put/2; the resulting set is passed into the next operation. section 2.4.11
  • Chain operations with the pipe operator because MapSet functions take the set first. section 4.1
  • Enumerate a set with Enum functions when iteration is required. section 2.4.11

A set of days

days = MapSet.new([:monday, :tuesday, :wednesday]) MapSet.member?(days, :monday) # true days = MapSet.put(days, :thursday) Enum.each(days, &IO.puts/1) section 2.4.11

iex(1)> days =
          MapSet.new() |>                #1
          MapSet.put(:monday) |>         #2
          MapSet.put(:tuesday)           #2
 
iex(2)> MapSet.member?(days, :monday)    #3
true

Follow the pipe-based construction of a MapSet and test membership against the resulting abstraction. section 4.1

How Elixir in Action, Third Edition covers it

How deep it goes
Explains construction, membership, insertion, enumeration, piping, and pure-Elixir implementation while deferring internal representation.
Examples
Demonstrates a set of weekday atoms, membership checks, insertion, pipeline construction, and enumeration.
What it assumes
Assumes familiarity with Elixir collections and modules while demonstrating abstraction boundaries and pipe-friendly APIs.
What you can do after
Apply via MapSet.new/1, member?/2, put/2, and Enum functions to construct and use unique-value collections.
How it connects
Connects MapSet with sets, maps, modules, built-in data types, enumeration, and the pipe operator.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Elixir in Action, Third Edition. The section links point to the parts of the book it draws on.