ijverig
Matching argument and map key in function parameters
We can match arguments like this in elixir:
iex(1)> f = fn(a, [b: a]) -> a end
#Function<12.99386804/2 in :erl_eval.expr/5>
iex(2)> f.(:me, b: :me)
:me
iex(3)> f.(:me, a: :me)
** (FunctionClauseError)
We can even match on keys of Keywords:
iex(3)> f = fn(a, [{a, a}]) -> a end
#Function<12.99386804/2 in :erl_eval.expr/5>
iex(4)> f.(:me, me: :me)
:me
iex(5)> f.(:me, mee: :me)
** (FunctionClauseError)
However we cannot match on keys in Maps:
iex(5)> f = fn(a, %{a => a}) -> a end
** (CompileError) iex:5: illegal use of variable a inside map key match, maps can only match on existing variables by using ^a
(stdlib) lists.erl:1354: :lists.mapfoldl/3
(stdlib) lists.erl:1354: :lists.mapfoldl/3
(stdlib) lists.erl:1355: :lists.mapfoldl/3
- Is there a way to match on keys in Maps?
- Why can we do it with Keywords but not with Maps?
Most Liked
idi527
You can use the pin operator ^ for this.
a = 1
case %{1 => :value} do
%{^a => value} -> value
end
So f = fn(a, %{a => a}) -> a end might be “rewritten” as
f = fn a, map -> case map do %{^a => ^a} -> a end end
iex(2)> f.(1, %{1 => 1})
1
Note, however, you’d need to add a case which would handle unmatched maps as well.
2
ijverig
Sure. Impossible to match on the parameters, then.
2
idi527
It’s currently impossible to match on “variable” map keys when passed as function arguments, yes.
It is possible to match on map values for predefined keys
def f(%{a: a}, %{a: a}), do: true
def f(_m1, _m2), do: false
1
Popular in Questions
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
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
Hi,
is there any work on GUI with Elixir, that is similar to Electron/Javascript? My idea is to bundle Phoenix and BEAM into a single se...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
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
Hi guys, i’m new in the Elixir world, and i have to say, that i love it!
i’m having some problem to understand anonymous functions with ...
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
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size?
Thanks
New
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Other popular topics
Here are few pieces of (common) Linux knowledge that we use for reasonably small one server apps. We use Ubuntu but this should work for ...
New
Hello, I get Persian date from my client and convert it to normal calendar like this:
def jalali_string_to_miladi_english_number(persi...
New
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set?
Thanks.
New
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
I have a User schema with a :from_id field set to type :string:
defmodule TweetBot.Repo.Migrations.CreateUsers do
use Ecto.Migration
...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
I will often find my self writing things similar to:
case some_value do
nil -> something()
"" -> something()
_ -> someth...
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
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New







