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 [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
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 [email protected].
For more options, visit https://groups.google.com/d/optout.