Akamee
Akamee

Reputation: 593

Join clojure sets on multiple keys

Is there a way to join clojure sets on multiple keys ? For exemple, I have set1 and set2 . I want to join the based on keys :a and :b

(def set1 #{ {:a 1 :b 2 :c 3} {:a 3 :b 4 :c 5} }) (def set2 #{ {:a 1 :b 2 :d 4} {:a 3 :b 4 :d 6} {:a 7 :b 7} {:a 1 :b 2 :c 4}})

desired output is

#{{:a 1 :b 2 :c 3 :d 4} {:a 1 :b 2 :c 4} {:a 3 :b 4 :c 5 :d 6} }

Upvotes: 1

Views: 188

Answers (2)

Leira Hua
Leira Hua

Reputation: 175

Seems like simple

cljs.user=> (clojure.set/join set1 set2)
#{{:a 1, :b 2, :c 3, :d 4}
  {:a 3, :b 4, :c 5, :d 6}
  {:a 1, :b 2, :c 4}}

did the same.

Upvotes: 0

Akamee
Akamee

Reputation: 593

One way to do so is to use clojure.set/join :

(clojure.set/join set1 set2 {:a :a :b :b})

It joins set1 and set2 based on the map {:a :a :b :b} which means it compares the value of :a in the firs map to the value of :a on the second map and the value of :b in the firs map to the value of :b on the second map

Upvotes: 5

Related Questions