marioalvial

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

Ankhers

Ankhers

It sounds like you want to use Map.pop/3

iex(1)> {value, map} = Map.pop(%{"One" => 1, "Three" => 3, "Two" => 2}, "One")
{1, %{"Three" => 3, "Two" => 2}}
iex(2)> value
1
iex(3)> map
%{"Three" => 3, "Two" => 2}

Also Liked

dorian-marchal

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}}
joaoevangelista

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.

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
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
baxterw3b
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
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
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41989 114
New
shahryarjb
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
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
joeerl
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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
romenigld
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
openscript
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement