June 2022
Intermediate to advanced
130 pages
2h 45m
English
Most pattern matching uses simple deconstruction, but not all. Sometimes, Elixir code needs to make allowances for special cases. One special case is comparing elements within a function head. Let’s take a few examples.
Let’s look at some special cases of pattern matching. The first is representing exact matches of primitive data within a data structure like a tuple. When you use primitive data in a function head, Elixir will try to match it exactly. An Elixir function to check and see if a point is the origin would look like this:
| | def origin?({0,0}), do: true |
| | def origin?(_point), do: false |
The function has two heads. The first matches the exact {0, 0} tuple, returning ...
Read now
Unlock full access