There is clojure.set/join to do exactly what you are asking for:

```
(-> (clojure.set/join entities locations)
    (clojure.set/rename {:id :location}))
;=> ({:name "bar", :location 101} {:name "foo", :location 100})
```

(I'm also using clojure.set/rename to rename :id to :location)

http://clojuredocs.org/clojure.set/join

(join xrel yrel)
(join xrel yrel km)

When passed 2 rels, returns the rel corresponding to the natural
join. When passed an additional keymap, joins on the corresponding
keys.

On Sun, Sep 13, 2015 at 11:14 AM, Brian Platz <bpl...@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 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.

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

Reply via email to