I want to merge lists of maps. Each map entry has an :id and :code
key. The code associated to :id from one map should have  higher
precedence than the same :id entry from another map.

I have an implementation. The problem and solution is best described
using example:

;priority 1 map
(def p1 [{:id 1 :code "m1a"} {:id 2 :code "m1b"} {:id 3 :code "m1c"}])
;priority 2 map
(def p2 [{:id 2 :code "m2b"} {:id 3 :code "m2c"} {:id 4 :code "m2d"}])
;priority 3 map
(def p3 [{:id 3 :code "m3c"} {:id 4 :code "m3d"} {:id 5 :code "m3e"}])
;priority 4 map
(def p4 [{:id 4 :code "m4d"} {:id 5 :code "m4e"} {:id 6 :code "m4f"}])

(defn merge-two [p1 p2]
"merge two maps giving higher priority (for any common :id) to p1 than
p2"
  (reduce
    (fn [result-list rec]
      (if (not-any? #(= (:id %) (:id rec)) result-list)
        (conj result-list rec)
        result-list))
    p1 p2))

(defn merge-all [ps]
"merge all maps with priority based on order"
  (reduce merge-two ps))

user => (merge-all [p1 p2 p3 p4])
[{:code "m1a", :id 1}
 {:code "m1b", :id 2}
 {:code "m1c", :id 3}
 {:code "m2d", :id 4}
 {:code "m3e", :id 5}
 {:code "m4f", :id 6}]

I have written the implementation. Is there a more efficient, cleaner
and idiomatic way to do this. Am I missing out on any core library
functions that already provide this behavior?

Also my lists can be very large in size - about 10,000 records (id and
value) each - so the solution must be efficient for large lists.

Thanks
Shoeb

-- 
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

Reply via email to