Sometimes it helps to change the structure of the data you are using to fit the job and simplify matters. In this case it appears that you are trying to represent a relation. However the tuples are being represented in a non-associative structure (a list). If you represent the relation using an outer map of inner maps then the job becomes as simple as
(merge data-orig data-override) The simplest way to do this is to use the value of the key (in this case :id) as the key for the outer map. You also ensure the uniqueness of the key in the resulting outer map. If you need to keep the data in the list structure you can do the transform to a relation using (use 'clojure.set) (index data-orig [:id]) though this solution will give you keys in the form {:id 2} and each value as a set (for one to many relations whereas in this case you only need one-one). Using a proper relational approach the full solution is simply (def data-orig {2 {:id 2 :a2 34 :a3 76 :a4 87} 3 {:id 3 :a2 30 :a3 38 :a4 39} 5 {:id 5 :a2 67 :a3 32 :a4 38} 4 {:id 4 :a2 10 :a3 73 :a4 38} 7 {:id 7 :a2 84 :a3 86 :a4 63}}) (def data-override {2 {:id 2 :a2 5534 :a3 5576 :a4 5587} 3 {:id 3 :a2 5584 :a3 5586 :a4 5563} 12 {:id 12 :a2 5593 :a3 5512 :a4 5539} 13 {:id 13 :a2 5509 :a3 5539 :a4 5592}}) (merge data-orig data-override) On Apr 26, 5:23 am, "Bhinderwala, Shoeb" <sabhinderw...@wellington.com> wrote: > Can someone help me write a merge function between two maps? My problem > is as follows: > > I have original data in a map: > > (def data-orig > '({:id 2 :a2 34 :a3 76 :a4 87}, > {:id 3 :a2 30 :a3 38 :a4 39}, > {:id 5 :a2 67 :a3 32 :a4 38}, > {:id 4 :a2 10 :a3 73 :a4 38}, > {:id 7 :a2 84 :a3 86 :a4 63})) > > Then I have override data: > > (def data-override > '({:id 2 :a2 5534 :a3 5576 :a4 5587}, > {:id 3 :a2 5584 :a3 5586 :a4 5563}, > {:id 12 :a2 5593 :a3 5512 :a4 5539}, > {:id 13 :a2 5509 :a3 5539 :a4 5592})) > > The result should be a merge of the two with the following conditions: > If the id is the same, should override the original data. If id is not > present in original then it should be added. The result should be: > > '({:id 2 :a2 5534 :a3 5576 :a4 5587}, ;overriden > {:id 3 :a2 5584 :a3 5586 :a4 5563}, ;overriden > {:id 5 :a2 67 :a3 32 :a4 38}, > {:id 4 :a2 10 :a3 73 :a4 38}, > {:id 7 :a2 84 :a3 86 :a4 63}, > {:id 12 :a2 5593 :a3 5512 :a4 5539}, ;added > {:id 13 :a2 5509 :a3 5539 :a4 5592}) ;added > > Thanks for your help. > -- 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