Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two complex data structures

Tags:

clojure

I'm having trouble finding solution to the following problem:

Lets say I have a map:

(def defaults {
  :name    "John"
  :surname "Doe"
  :info    {:date-of-birth "01-01-1980"
            :registered    [{:type "newsletter" :name "breaking news" }]}
})

And then I pass a similar structured map but I want to conjoin the vectors and overwrite the rest of the keys:

(def new {
  :name    "Peter"
  :info    {:date-of-birth "11-01-1986"
            :registered    [{:type "alert" :name "mobile-alert" }]}
})

And I want this result:

 {:name    "Peter"
  :surname "Doe"
  :info    {:date-of-birth "11-01-1986"
            :registered    [{:type "newsletter" :name "breaking news" }
                            {:type "alert"      :name "mobile-alert" }]}}

Now I can do this easily by using static syntax like:

(reduce conj (get-in defaults [:info :registered]) (get-in new [:info :registered]))

(There is probably a better way...) But I was hoping more of a dynamic function with the following properties:

  1. Keep all keys from both maps, without knowing the structure
  2. Update any keys with the values from the right map
  3. if the val of a key is a vector, then conj the vector with the vector of the right map (if the appropriate key exists of course)

Thanks for the help in advance :)

like image 754
Dimitrios K. Avatar asked Jun 26 '13 18:06

Dimitrios K.


1 Answers

You should definitely look at merge-with function. This is possible implementation:

(defn deep-merge [a b]
  (merge-with (fn [x y]
                (cond (map? y) (deep-merge x y) 
                      (vector? y) (concat x y) 
                      :else y)) 
                 a b))
like image 95
Slartibartfast Avatar answered Sep 26 '22 07:09

Slartibartfast