vitalydolgov

vitalydolgov

Pattern matching in `case` for maps

I have a question on pattern matching in case. This function

def f(map) do
  case map do
    %{a: i} -> i
    %{} -> :empty
    _ -> :other
  end
end

works in this way:

> f(%{a: 67})
67
> f(%{b: 67})
:empty
> f(%{})
:empty

Can anybody explain me why it matches on the second clause, when non-empty map provided? I’m on Elixir 1.11.

Marked As Solved

Werner

Werner

It is also in the hexdocs the reference for it, in the section on maps:

“Note that the empty map will match all maps,…”

https://hexdocs.pm/elixir/master/patterns-and-guards.html

Also Liked

RudManusachi

RudManusachi

Hi @vitalydolgov :wave:t2: And welcome to the community!

%{} matches any map.

To match against empty map you could use either map_size/1 function or compare with %{} explicitly in guard as:

def f(map) do
  case map do
    # any map that has key :a
    %{a: i} -> i

    # empty map
    map when map == %{} -> :empty

    # map with exactly 1 key but not :a
    map when map_size(map) == 1 -> :one_key_not_a

    # any other map
    %{} -> :other

    # not a map
    _ -> :not_a_map
  end
end
dsounded

dsounded

Hey

But basically you just need to use a guard for the second case

Last Post!

vitalydolgov

vitalydolgov

Thank you :blush:

Where Next?

Popular in Questions Top

nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

We're in Beta

About us Mission Statement