silverdr

silverdr

Updating Map with "shortcut syntax"

I often see the “shortcut syntax” for updating maps / replacing values using pipe character being advocated. Something like:

iex(19)> %{ %{a: 0, b: 1, c: 2, d: 3} | c: 3, d: 4}
%{a: 0, b: 1, c: 3, d: 4} 

OTOH I vaguely recall reading somewhere about this being not a thing to rely on due to … <and here I forgot the explanations and can’t find where I read them>

Documentation mentions this syntax, however: " Maps also support a specific update syntax to update the value stored under existing atom keys:", so… are/were there any caveats/gotchas in using the pipe for updating values of existing keys?

Most Liked

brettbeatty

brettbeatty

syntax to update the value stored under existing atom keys

It’s interesting the documentation calls out atom keys specifically. You can also use it to update non-atom keys (though the syntax is slightly different, I suppose):

iex(1)> my_map = %{"a" => 3, "b" => 4}
%{"a" => 3, "b" => 4}
iex(2)> %{my_map | "b" => 5}
%{"a" => 3, "b" => 5}
kip

kip

ex_cldr Core Team

Shortcut syntax is particularly concise when dealing with structs since they fulfil the two preconditions definitively: (a) atom keys and (b) keys are known to exist.

They are less useful with dealing with arbitrary maps because one or both of the preconditions for using shortcut syntax can’t be assumed.

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31586 112
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement