The Pragmatic Studio

Pattern Matching

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 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, it's 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. (Wow, that sounded scientific!)

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 video is in the parse directory found within the video-code directory of the code bundle.

All course material, including videos and source code, is copyrighted and licensed for individual use only. You may make copies for your own personal use (e.g. on your laptop, on your iPad, on your backup drive). However, you may not transfer ownership or share the material with other people. We make no guarantees that the source code is fit for any purpose. Course material may not be used to create training material, courses, books, and the like. Please support us by encouraging others to purchase their own copies. Thank you!

Copyright © 2005–2024, The Pragmatic Studio. All Rights Reserved.