Pattern Matching in Elixir
Notes
What’s a Term?
In the video, we used the word term as in “match the term on the right-hand side.” If you poke around the Elixir documentation, you’ll see references to the word term as well.
A term is a value of any data type: a string, an atom, a map, a list, etc.
Shortcut: Using Atoms As Map Keys
Elixir atoms are prefixed by a colon. For example, here’s a map that uses atoms as keys:
%{ :method => "GET", :path => "/wildthings" }However, its so common to use atoms as keys that Elixir gives us a shortcut. And we love shortcuts, so we used it in the video:
%{ method: "GET", path: "/wildthings" }This form is more concise: we removed the => and put the colon (:) on the other side of the atom.
If the keys are anything but atoms, you must use the general => form. For example, here’s a map with strings as keys:
%{ "method" => "GET", "path" => "/wildthings" }Code So Far
The code for this section is in the parse directory found within the video-code directory of the code bundle
Why always me?