merge nested maps

3,846 views
Skip to first unread message

Joachim De Beule

unread,
Apr 25, 2013, 4:41:33 PM4/25/13
to clo...@googlegroups.com
Hi list,

I was searching for an "easy" way to combined nested maps, e.g. as in 

(combine {:foo {:bar "baz"}} {:foo {:x "y"}})
=> {:foo {:bar "baz", :x "y"}}

I would expect that there is some core map operation to do this, but neither merge nor unify work as they simply return {:foo {:x "y"}}, and I don't see anything else. Am I missing something?

Joachim.

Michael Gardner

unread,
Apr 25, 2013, 4:54:52 PM4/25/13
to clo...@googlegroups.com
On Apr 25, 2013, at 15:41 , Joachim De Beule <joachim....@gmail.com> wrote:

> I was searching for an "easy" way to combined nested maps, e.g. as in
>
> (combine {:foo {:bar "baz"}} {:foo {:x "y"}})
> => {:foo {:bar "baz", :x "y"}}

user=> (merge-with merge {:foo {:bar "baz"}} {:foo {:x "y"}})
{:foo {:x "y", :bar "baz"}}

If you need to support more than one level of nesting, you could try something like:

(defn combine [& maps]
(apply merge-with combine maps))

Cedric Greevey

unread,
Apr 25, 2013, 4:59:22 PM4/25/13
to clo...@googlegroups.com
Seems you want a cross between update-in and merge. Maybe something like this?

(defn combine
  "Merge maps, recursively merging nested maps whose keys collide."
  ([] {})
  ([m] m)
  ([m1 m2]
    (reduce (fn [m1 [k2 v2]]
              (if-let [v1 (get m1 k2)]
                (if (and (map? v1) (map? v2))
                  (assoc m1 k2 (combine v1 v2))
                  (assoc m1 k2 v2))
                (assoc m1 k2 v2)))
            m1 m2))
  ([m1 m2 & more]
    (apply combine (combine m1 m2) more)))

(warning: scarcely tested much)



--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jorge Urdaneta

unread,
Apr 25, 2013, 5:13:49 PM4/25/13
to clo...@googlegroups.com
It was fun to try a naive implementation

(defn combine [m1 m2]
  (let [mm1 (transient m1)]
    (do
      (doseq [k (keys m2)]
        (if (contains? m1 k)
          (assoc! mm1 k (conj (mm1 k) (m2 k)))                                                                          
          (assoc! mm1 k (m2 k))))
      (persistent! mm1))))

-- 
Jorge Urdaneta

Stuart Sierra

unread,
Apr 25, 2013, 5:26:53 PM4/25/13
to clo...@googlegroups.com
Here's a way to do it from the Pedestal demo source code:

(defn deep-merge
  "Recursively merges maps. If keys are not maps, the last value wins."
  [& vals]
  (if (every? map? vals)
    (apply merge-with deep-merge vals)
    (last vals)))

-S

Gary Verhaegen

unread,
Apr 28, 2013, 4:55:45 AM4/28/13
to clo...@googlegroups.com
Shouldn't that docstring read "If vals are not maps" ?

Stuart Sierra

unread,
Apr 28, 2013, 10:19:57 AM4/28/13
to clo...@googlegroups.com
Yes.


You received this message because you are subscribed to a topic in the Google Groups "Clojure" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/clojure/UdFLYjLvNRs/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to clojure+u...@googlegroups.com.

Chris Gill

unread,
Jul 17, 2013, 8:42:24 AM7/17/13
to clo...@googlegroups.com
Thank you for finding and posting this! Been wracking my brain trying to figure out a good way to do a full map merge

JvJ

unread,
Jul 17, 2013, 2:53:03 PM7/17/13
to clo...@googlegroups.com

Uwe Dauernheim

unread,
Jul 18, 2013, 6:52:00 AM7/18/13
to clo...@googlegroups.com
The implementation(s) acts a bit counter-intuitive on non-nested maps:

FAIL in (deep-merge-test) (test.clj:5)
expected: (= (merge {:a 1, :d 1} {:a 2, :c 1}) (deep-merge {:a 1, :d 1} {:a 2, :c 1}))
  actual: (not (= {:c 1, :a 2, :d 1} {:a 2, :c 1}))

ERROR in (deep-merge-with-test) (Numbers.java:942)
expected: (= nil (deep-merge-with + nil nil nil))
  actual: java.lang.NullPointerException: null
Reply all
Reply to author
Forward
0 new messages