You’re right, I missed a step :-). Something like (typed in-line so it really won’t compile):
:locations [{“us” 100} {“de” 101}] :locations-seq (into {} (map (fn [{:keys [location]}] [:location (get locations location)])…) ;; the above produces a sequence symmetrical with entities, each map containing {:location id} where id is the correct ID for the matched entity). (map #(apply merge) entities locations-seq) Your solution is nicer and more efficient and doesn’t rely on ordering either. That will teach me for multi-tasking :-). > On 13 Sep 2015, at 5:09 PM, Brian Platz <bpl...@gmail.com> wrote: > > 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 >> <http://gmail.com/>> 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 >> <http://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+u...@ <>googlegroups.com <http://googlegroups.com/> >> For more options, visit this group at >> http://groups.google.com/group/clojure?hl=en >> <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 <http://googlegroups.com/>. >> For more options, visit https://groups.google.com/d/optout >> <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 > <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+unsubscr...@googlegroups.com>. > For more options, visit https://groups.google.com/d/optout > <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.