liveBook · Manning

Match pattern

As explained in Elixir in Action, Third Edition

An ETS match pattern is a tuple-shaped descriptor that specifies which table rows should match, with wildcard atoms representing unconstrained fields.

An ETS match pattern is a tuple-shaped descriptor for selecting rows by position. The atom :_ leaves a field unconstrained, so {:_, "Dentist"} selects every row whose second element is "Dentist". Unlike ordinary Elixir pattern matching, the tuple is interpreted by :ets.match_object/2. section 10.3.3

Finding appointments by type

With rows such as {~D[2023-05-24], "Dentist"} and {~D[2023-05-30], "Dentist"}, calling :ets.match_object(todo_list, {:_, "Dentist"}) returns both appointments, regardless of date. The related :ets.match_delete/2 operation removes multiple matching objects in one statement. section 10.3.3

iex(6)> :ets.match_object(todo_list, {:_, "Dentist"})
[{~D[2023-05-24], "Dentist"}, {~D[2023-05-30], "Dentist"}]

Inspect the runnable ETS query that matches all dentist appointments across dates. section 10.3.3

How Elixir in Action, Third Edition covers it

How deep it goes
Defines tuple shape, wildcard atoms, API interpretation, and copying benefits, but does not detail complete match-spec internals.
Examples
Uses a bag ETS todo list containing dated appointments and queries all rows labeled "Dentist".
What it assumes
Assumes ETS tables and tuple-shaped rows, while clarifying why :_ replaces Elixir's ordinary anonymous variable.
What you can do after
Apply it via Erlang's ETS API to retrieve or delete rows matching selected tuple fields.
How it connects
Connects ETS tables, tuple rows, table traversal, ordinary pattern matching, and match_object/2.

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.