How to check if an Elixir map has a given key in a guard?

Article autor
December 22, 2022
How to check if an Elixir map has a given key in a guard?
Elixir Newsletter
Join Elixir newsletter

Subscribe to receive Elixir news to your inbox every two weeks.

Oops! Something went wrong while submitting the form.
Elixir Newsletter
Expand your skills

Download free e-books, watch expert tech talks, and explore open-source projects. Everything you need to grow as a developer - completely free.

Table of contents

Today's Advent of Code puzzle inspired me to create this TIL. It may sound trivial, but in fact, it's tricky if you are unfamiliar with the nuances of guards' functioning.

Usually, you would write Map.has_key?(map, key) , but it's forbidden in guards since, in them, you can only use expressions from a strictly limited list.

** (CompileError) iex:11: cannot invoke remote function Map.has_key?/2 inside guards
    (elixir 1.13.1) src/elixir_fn.erl:17: anonymous fn/4 in :elixir_fn.expand/4
    (stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3
    (elixir 1.13.1) expanding macro: Kernel.|>/2

What about in?

Unfortunately, unlike in languages like Python or Javascript, Elixir's inmacro doesn't check for key inclusion in a given map.

is_map_key/2 for the help

Elixir 1.10.0 introduced a new function allowed in guard tests - is_map_key/2 .

Let's see an example:

iex(1)> variables = %{"a" => 5}
...(1)> translated = Enum.map(["a", "b"], fn
...(1)>   name when is_map_key(variables, name) -> variables[name]
...(1)>   name -> name
...(1)> end)
[5, "b"]

This code replaces all occurrences of known variable names in a given list with their values.

You probably don't need an is_map_key/2

Constructions like the above are rather rare. In most cases, you can get the job done using good old pattern matching.

If you already know which key you are looking for, a much better option is to create multiple function clauses:

def has_user?(%{user: _}), do: true
def has_user?(_), do: false

Work with a team that keeps learning and building better software every day.

Related posts

Dive deeper into this topic with these related posts

No items found.

You might also like

Discover more content from this category

How to run tests in Elixir IEx shell

Hey! Have you ever wondered about tests running inside the IEx shell? For a long time, I was convinced that it’s not really possible. And as it turns out - that’s not really straightforward. You won’t easily find information about that in the documentation.

Why & when you should use PostgreSQL deferred uniqueness constraints

Learn a trick that will allow you to manage item order in Postgres tables easier & faster.

Implicit try in Elixir

In the world of Elixir programming, there are numerous features and syntactic constructs that contribute to the language's elegance and expressiveness. One such hidden gem is the concept of "implicit try".