marioalvial
Get specific key and the rest of map with Pattern Matching in Elixir
Im trying to get one element and the rest of the map with pattern matching but I’m only get compile errors.
I came up with this:
%{“One” => one | tail} = %{“One" => 1, "Three" => 3, "Two" => 2}
But I got compile errors saying that it was expected key-value pairs.
The behavior that I’m trying to achieve is:
%{“One” => one | tail} = %{“One" => 1, "Three" => 3, "Two" => 2}
one = 1
tail = %{"Three" => 3, "Two" => 2}
In elixir there is a way to acomplished that?
Marked As Solved
Also Liked
dorian-marchal
A bit late to the party, but if you want to pattern match in a function definition, you can also capture the whole map:
defmodule Foo do
def foo(map = %{"One" => one}) do
rest = Map.delete(map, "One")
IO.inspect(%{one: one, rest: rest})
end
end
Foo.foo(%{"One" => 1, "Three" => 3, "Two" => 2})
# > %{one: 1, rest: %{"Three" => 3, "Two" => 2}}
3
joaoevangelista
Note that you get that error because on %{"One" => one | tail} the | is used to update the key, in your case is like that you are trying to update the tail key but missing the value.
2
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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I have a list say
x = ["23gh", "56kh", "97mh"]
I would like to pass each element to Val in each iteration.
Say, in iteration 1 -------...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Sometimes I want to check if the input into a function is not a blank string.
My first approach:
defmodule Example do
def do_stuff(s...
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
Other popular topics
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
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 would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
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
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
Hello!
Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
Hi folks,
Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
New
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
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
After calling mix ecto.create I get this error:
17:00:32.162 [error] GenServer #PID<0.412.0> terminating
** (Postgrex.Error) FATAL...
New







