I have wrote a version for this in our project. It behaves exactly like Map.merge/2 but concating lists and merging inner maps:
``` # @spec merge_cat(map, map) :: map def merge_cat(e, acc), do: Map.merge(acc, e, &combiner/3) defp combiner(_, l1, l2) when is_list(l1) and is_list(l2), do: l1 ++ l2 defp combiner(_, m1, m2) when is_map(m1) and is_map(m2), do: merge_cat(m1, m2) defp combiner(_, _, v2), do: v2 ``` On Wed, Sep 7, 2016 at 6:02 AM, Brian Cardarella <br...@dockyard.com> wrote: > This is assuming that all nested values are maps all the way down. What > happens in the event of a Keyword list for example? Or a tuple? If the > use-case is limiting itself to just maps of maps this works. But if the > wider use-case should be supported it may make sense to consider a > Kernel.deep_merge that has indifferent access support for the values to be > merged. > > > On Tuesday, September 6, 2016 at 2:41:22 PM UTC-4, OvermindDL1 wrote: >> >> I had made my own a few times, though rare, it could be useful. Unsure >> about it in core but in a library definitely. >> >> On Tuesday, September 6, 2016 at 9:20:23 AM UTC-6, Tobias Pfeiffer wrote: >>> >>> Hi everyone, >>> >>> in Elixir we work a lot with maps (or at least I do :D). And we know and >>> love Map.merge. However given a map like %{a: %{b: 1}} and we want to >>> merge it with a map like %{a: %{c: 2}} then the :a key will be totally >>> overridden with whatever is supplied in the other map. >>> >>> However, for instance for configuration, what we might want is the >>> result to be %{a: %{b: 1, c: 2}} - e.g. if the value is a map on both >>> sides recursively merge that as well. >>> >>> It is rather simple to implement in elixir with Map.merge/3 [1] but I >>> think it'd still be helpful to have it in elixir core (otherwise I'll >>> make a mini hex package :D). E.g. it is useful and it'll keep people >>> from wondering about it or writing their own a little flawed >>> implementations. >>> >>> So, what do you think - Map.deep_merge in elixir-core - yay/nay? I'm >>> happy to draft a PR if it gets the sign of approval. >>> >>> Tobi >>> >>> >>> [1] http://stackoverflow.com/a/38865647/1810896 >>> -- >>> http://www.pragtob.info/ >>> >> -- > 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-core+unsubscr...@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/ > msgid/elixir-lang-core/c451058c-6a49-481e-9cb8- > 1541ce1b77a7%40googlegroups.com > <https://groups.google.com/d/msgid/elixir-lang-core/c451058c-6a49-481e-9cb8-1541ce1b77a7%40googlegroups.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- 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-core+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CACh6Pr6xCnOe_Brj%2Bt2yohpuDzJ6ZOPphOdwkrUc1YMEr%2BKfow%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.