redhands
redhands

Reputation: 357

update list of map value with matching value from another list of map

I have two datasets similar to the below. I am trying to update the id value in map2 to the corresponding di2 value in map1.

map1({id1:1 value:2 :valueb:4 id2:a}{id1:2 value:2 :valueb:4 id2:b}{id1:3 value:2 :valueb:4 id2:c})
map2({id:1 x:1 y:2}{id:2 x:4 y:6}{id:3 x:1 y:3})

So the output would look like:

map3({id:a x:1 y:2}{id:b x:4 y:6}{id:c x:1 y:3})

Any help appreciated, not sure how to go about this one..

Thanks, D

Upvotes: 0

Views: 67

Answers (1)

Thumbnail
Thumbnail

Reputation: 13473

Assuming that your data is really

(def map1 (list {:id1 1, :value 2, :valueb 4, :id2 'a}
                {:id1 2, :value 2, :valueb 4, :id2 'b}
                {:id1 3, :value 2, :valueb 4, :id2 'c}))

(def map2 (list {:id 1, :x 1, :y 2}
                {:id 2, :x 4, :y 6}
                {:id 3, :x 1, :y 3}))

... then

(let [im (into {} (map (juxt :id1 :id2) map1))]
  (map #(assoc % :id (im (% :id))) map2))

... produces what you are looking for:

; ({:y 2, :x 1, :id a} {:y 6, :x 4, :id b} {:y 3, :x 1, :id c})

First, we build, in im, a map of the required translations from map1; then we apply it, map by map, to the :id entries in map2.

This is pretty clearly a relational algebra thing, but I can't find a standard function or combination of functions to fit. Here might be a reasonable place to look.

Upvotes: 2

Related Questions