{Keyword,Map}.merge_new/2

28 views
Skip to first unread message

Austin Ziegler

unread,
Mar 4, 2021, 11:29:17 AM3/4/21
to elixir-l...@googlegroups.com
This isn’t something I’ve needed often, but every time I have needed
it, I have to look up the docs of merge/3. Basically, I want to merge
two keyword lists or maps but only add new values (do not replace old
values). I know that this is just:

```elixir
Keyword.merge(left, right, fn _k, lv, _rv -> lv end)
```

But I have to look it up in the docs every time; I think that adding
the new name here would be good UX. Alternatively, the accepted values
for `merge/3` could be changed to accept a conflict function or an
atom indicating the merge strategy. This is, IMO, worse UX because it
would also be incompatible with older versions (delaying its adoption
by libraries).

-a
--
Austin Ziegler • halos...@gmail.comaus...@halostatue.ca
http://www.halostatue.ca/http://twitter.com/halostatue

José Valim

unread,
Mar 4, 2021, 11:32:17 AM3/4/21
to elixir-l...@googlegroups.com
I believe the most efficient way to do what you want is to reverse the order of arguments and call Map.merge/2 and Keyword.merge/2.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAJ4ekQveKLuvrwyRwMCB-%2B_b6m%2BiZJgx%3D4EXfUs5AYe-Nb0Keg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages