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
It is also in the hexdocs the reference for it, in the section on maps:
“Note that the empty map will match all maps,…”
2
Also Liked
RudManusachi
Hi @vitalydolgov
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
4
dsounded
Hey
But basically you just need to use a guard for the second case
1
Popular in Questions
can someone please explain to me how Enum.reduce works with maps
New
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
Hello everybody,
usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
Hi guys, nice to meet you to the whole forum, I’m new here, I’m trying to configure visual studio code for elixir, right now the intellis...
New
To simplify some tasks at work, I wrote and published this package yesterday. It’s a simple macro that enables Access behaviour on struct...
New
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: )
Hello all, this is ...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Other popular topics
can someone please explain to me how Enum.reduce works with maps
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
As a follow up to my earlier question:
I have the code compiling and running but not getting a successful login from the rest server. ...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hello, I have map which I want to convert it to string like this:
the map:
%{last_name: "tavakkoli", name: "shahryar"}
the string I ne...
New
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New







