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

Where Next?

Popular in Questions Top

Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
johnnyicon
Hi all, I’ve just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I’m trying to use Postgres...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

Other popular topics Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
klo
Got a question about when to concat vs. prepending items to list then reversing to achieve appending. So i know lists boil down to [1 | ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

We're in Beta

About us Mission Statement