Not sure I follow Colin's suggestion entirely, but his suggestion led me to this which is better. I'd still love any other suggestions to improve!
(def entities [{:name "foo" :location "us"} {:name "bar" :location "de"}]) (def locations [{:location "us" :id 100} {:location "de" :id 101}]) ;; make an intermediary locations into a large single map of format {"us" 100 "de" 101} (def locations* (apply hash-map (mapcat vals locations))) (map #(assoc % :location (get locations* (:location %))) entities) => ({:name "foo", :location 100} {:name "bar", :location 101}) On Sunday, September 13, 2015 at 10:56:14 AM UTC-4, Colin Yates wrote: > > I would transform the locations into {location id} and then use merge. > > On 13 Sep 2015, at 3:14 PM, Brian Platz <bpl...@gmail.com <javascript:>> > wrote: > > I have a pattern that comes up frequently, when I need to merge some value > into one map list from matching keys in a second map list. > > I've developed a way to handle it, but I think it could be better. Here is > a simple example of it: > > In the below case, we are looking to replace the :location in entities > with the respective location id found in the second list: > > (def entities [{:name "foo" :location "us"} {:name "bar" :location "de"}]) > (def locations [{:location "us" :id 100} {:location "de" :id 101}]) > > (reduce (fn [entities loc] > (->> entities > (map (fn [entity] > (if (= (:location entity) (:location loc)) > (assoc entity :location (:id loc)) > entity))))) > entities locations) > > => ({:name "foo", :location 100} {:name "bar", :location 101}) > > This works, however it feels like there should be an easier way in clojure > that is at minimum more readable, and ideally even more performant. > > Any suggestions? > > Thanks! > > -- > 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 <javascript:> > 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 <javascript:> > 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 <javascript:>. > For more options, visit https://groups.google.com/d/optout. > > > -- 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/d/optout.