How To Pattern Match Maps In Elixir — Tentamen Software Testing Blog

Pattern Matching Elixir Maps


In the previous post, we explained how to make pattern matching on Elixir Lists. Today we move on pattern matching Elixir Maps. This post is part of the functional language series, and it is based on the remarkable book Elixir In Action by Sasa Juric.

Map Pattern Matching

In the screenshot above, we first recapped two ways how we define Elixir Map.

Then we did a pattern match, similar to Tuple Pattern Matching. It is important to state that on the pattern side (left side), we can only declare what we are interested in, as long that the Map key exists. For example, we can only pattern match type. This is called the partial matching rule.

And for the end, we have a Pattern Match error.

Originally published at on January 15, 2021.