1
votes

I have a two sets of maps similar to

[{:id 101 :price 40.00} {:id 102 :price 50.00} {:id 103 :price 30.00}]

[{:id 101 :price 45.00} {:id 102 :price 50.00} {:id 103 :price 20.00}]

I am trying to match each map in the first sequence with counterpart in the second based on the "id" key so that I can determine a new price.

The logic for getting the new price seems straightforward enough, but I can't seem to get the two maps to line up. I assumed the map function would be what I need but after spending some time in the REPL, I wasn't able to produce the desired result.

Could someone please point me in the right direction?

1

1 Answers

2
votes
(def data1 [{:id 101 :price 40.00} {:id 102 :price 50.00} {:id 103 :price 30.00}])
(def data2 [{:id 101 :price 45.00} {:id 102 :price 50.00} {:id 103 :price 20.00}])

(require '[clojure.set :as set])

(set/join data1 (map #(set/rename-keys % {:price :new-price}) data2))

;=> #{{:new-price 45.0, :price 40.0, :id 101}
;     {:new-price 20.0, :price 30.0, :id 103}
;     {:new-price 50.0, :price 50.0, :id 102}}