Re: merge nested maps
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 On Wednesday, July 17, 2013 8:53:03 PM UTC+2, JvJ wrote: By the way, this is already in contrib: http://clojuredocs.org/clojure_contrib/clojure.contrib.map-utils/deep-merge-with On Wednesday, July 17, 2013 5:42:24 AM UTC-7, Chris Gill wrote: Thank you for finding and posting this! Been wracking my brain trying to figure out a good way to do a full map merge On Thursday, April 25, 2013 5:26:53 PM UTC-4, Stuart Sierra wrote: Here's a way to do it from the Pedestal demo source codehttps://github.com/pedestal/demo/blob/17eeac7a5e50d31eb81901de465f3f1d863f2f01/hammock-cafe/src/hammock_cafe/config.clj#L37 : (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 On Thursday, April 25, 2013 4:41:33 PM UTC-4, Joachim De Beule wrote: 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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: merge nested maps
Thank you for finding and posting this! Been wracking my brain trying to figure out a good way to do a full map merge On Thursday, April 25, 2013 5:26:53 PM UTC-4, Stuart Sierra wrote: Here's a way to do it from the Pedestal demo source codehttps://github.com/pedestal/demo/blob/17eeac7a5e50d31eb81901de465f3f1d863f2f01/hammock-cafe/src/hammock_cafe/config.clj#L37 : (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 On Thursday, April 25, 2013 4:41:33 PM UTC-4, Joachim De Beule wrote: 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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: merge nested maps
By the way, this is already in contrib: http://clojuredocs.org/clojure_contrib/clojure.contrib.map-utils/deep-merge-with On Wednesday, July 17, 2013 5:42:24 AM UTC-7, Chris Gill wrote: Thank you for finding and posting this! Been wracking my brain trying to figure out a good way to do a full map merge On Thursday, April 25, 2013 5:26:53 PM UTC-4, Stuart Sierra wrote: Here's a way to do it from the Pedestal demo source codehttps://github.com/pedestal/demo/blob/17eeac7a5e50d31eb81901de465f3f1d863f2f01/hammock-cafe/src/hammock_cafe/config.clj#L37 : (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 On Thursday, April 25, 2013 4:41:33 PM UTC-4, Joachim De Beule wrote: 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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: merge nested maps
Shouldn't that docstring read If vals are not maps ? On 25 April 2013 23:26, Stuart Sierra the.stuart.sie...@gmail.com wrote: 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 On Thursday, April 25, 2013 4:41:33 PM UTC-4, Joachim De Beule wrote: 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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: merge nested maps
Yes. On Sun, Apr 28, 2013 at 4:55 AM, Gary Verhaegen gary.verhae...@gmail.comwrote: Shouldn't that docstring read If vals are not maps ? On 25 April 2013 23:26, Stuart Sierra the.stuart.sie...@gmail.com wrote: 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 On Thursday, April 25, 2013 4:41:33 PM UTC-4, Joachim De Beule wrote: 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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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 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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
merge nested maps
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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: merge nested maps
On Apr 25, 2013, at 15:41 , Joachim De Beule joachim.de.be...@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)) -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: merge nested maps
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) On Thu, Apr 25, 2013 at 4:41 PM, Joachim De Beule joachim.de.be...@gmail.com wrote: 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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: merge nested maps
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 On 25/04/13 16:29, Cedric Greevey wrote: 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) On Thu, Apr 25, 2013 at 4:41 PM, Joachim De Beule joachim.de.be...@gmail.com mailto:joachim.de.be...@gmail.com wrote: 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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@googlegroups.com mailto:clojure@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+unsubscr...@googlegroups.com mailto:clojure%2bunsubscr...@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+unsubscr...@googlegroups.com mailto:clojure%2bunsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out. -- Jorge Urdaneta -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.
Re: merge nested maps
Here's a way to do it from the Pedestal demo source codehttps://github.com/pedestal/demo/blob/17eeac7a5e50d31eb81901de465f3f1d863f2f01/hammock-cafe/src/hammock_cafe/config.clj#L37 : (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 On Thursday, April 25, 2013 4:41:33 PM UTC-4, Joachim De Beule wrote: 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. -- -- You received this message because you are subscribed to the Google Groups Clojure group. To post to this group, send email to clojure@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+unsubscr...@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+unsubscr...@googlegroups.com. For more options, visit https://groups.google.com/groups/opt_out.