liveBook · Manning

Match specification

As explained in Elixir in Action, Third Edition

An ETS match specification is a structured query containing a row pattern, guard filters, and a result expression.

A match specification turns an ETS query into a structured operation that filters rows in ETS memory and controls which fields are returned. This avoids copying every table row into the selecting process and supports richer queries than basic match patterns. section 10.3.3

A specification contains three parts:

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

Use match_object/2 for a simpler row-pattern query; move to a full specification when filtering or returned fields become more selective. section 10.3.3

How Elixir in Action, Third Edition covers it

How deep it goes
Defines heads, guards, and results, but defers complex syntax to :ets.select/2 documentation and ex2ms.
Examples
Demonstrates todo rows queried with :ets.match_object/2 before richer specifications are introduced.
What it assumes
Assumes familiarity with ETS tables and basic match patterns before adding guards and result shaping.
What you can do after
Apply it via :ets.select/2 to filter ETS rows and choose the returned fields.
How it connects
Links ETS tables with match patterns, guards, result expressions, and :ets.select/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.