Having a list of equally sized lists, e.g.:
(def d [["A" "B"] ["A" "C"] ["H" "M"]])
How can it be transformed into a list of sets, each set for the indexes above:
[#{"A" "H"} #{"B" "C" "M"}]
(map set (apply map vector d))
"(apply map vector)" is what is called "zip" in other languages like Python. It calls vector on the first item of each element of d, then the second item of each element, etc.
Then we call set on each of those collections.
if hash-set allowed duplicate keys, you could use:
(apply map hash-set d)
instead, you can do the uglier
(apply map (fn [& s] (set s)) d)
I'd suggest the following:
(reduce
(fn [sets vals]
(map conj sets vals))
(map hash-set (first d))
(rest d))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With