Is it possible to update the multiple values in a map with a single call

2,579 views
Skip to first unread message

David Simmons

unread,
Apr 11, 2015, 4:50:30 AM4/11/15
to elixir-l...@googlegroups.com
Hi.

I have a simple map m = %{name: "Fred", age 50}

I can update name using m = %{m | name: "Dave"}

but is it possible to update both name and age at the same time?

many thanks.

dave

Chris McGrath

unread,
Apr 11, 2015, 5:03:23 AM4/11/15
to elixir-l...@googlegroups.com, Christopher McGrath
You can update both using the update syntax:

iex(2)> m = %{m | name: "Dave", age: 25 }
%{age: 25, name: "Dave”}

The update fields must already exist in the map though:

iex(3)> m = %{m | name: "Dave", age: 25, foo: :bar }
** (ArgumentError) argument error
    (stdlib) :maps.update(:foo, :bar, %{age: 25, name: "Dave"})
    (stdlib) erl_eval.erl:255: anonymous fn/2 in :erl_eval.expr/5
    (stdlib) lists.erl:1261: :lists.foldl/3

HTH,

Chris

--
You received this message because you are subscribed to the Google Groups "elixir-lang-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-ta...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-talk/93bd0f85-a776-490e-8533-abed6711cc03%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

David Simmons

unread,
Apr 11, 2015, 5:06:48 AM4/11/15
to elixir-l...@googlegroups.com, ch...@chrismcg.com
Cheers Chris - I'd tried the option you suggested but must have had a type so failed for me.  Re-tried and it worked.

thanks for your quick response.

Dave 

David Simmons

unread,
Apr 11, 2015, 5:19:11 AM4/11/15
to elixir-l...@googlegroups.com, ch...@chrismcg.com
Sorry Chris.

One more question if I may. I have a second map x = %{name: "Bart", age: 20}. I thought I might be able to update my first map with:

%{ m | x} but this results in an error. Any idea how I pass in a map as a 2nd argument?

cheers

Dave

Chris McGrath

unread,
Apr 11, 2015, 5:28:24 AM4/11/15
to David Simmons, elixir-l...@googlegroups.com
Map.merge(m, x) should do what you want here. I think it’s not working because %{} is a special form and so it doesn’t dereference the variable. That’s a total guess though!

Chris

David Simmons

unread,
Apr 11, 2015, 5:37:17 AM4/11/15
to elixir-l...@googlegroups.com, shortl...@gmail.com
Perfect - many thanks.

Ben Wilson

unread,
Apr 11, 2015, 10:50:30 AM4/11/15
to elixir-l...@googlegroups.com, shortl...@gmail.com
Do keep in mind that unlike %{ | } Map.merge CAN introduce new keys in your map if there are keys in the second map not currently present in the first.

On Saturday, April 11, 2015 at 5:37:17 AM UTC-4, David Simmons wrote:
Perfect - many thanks.

David Simmons

unread,
Apr 11, 2015, 11:41:48 AM4/11/15
to elixir-l...@googlegroups.com, shortl...@gmail.com
@Ben - did think about that but feel it is a small price to pay :-).




Reply all
Reply to author
Forward
0 new messages