Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

intersection between two maps in clojure

I have hash-maps and I'm trying to get the intersection between the keys. But the function 'intersection' just work with sets.

So I'm getting the keys, transforming the keys to sets and then calling the intersection, something like this:

(intersection (set (keys map-1)) (set (keys map-2)))

There are a better way to do that?

Thanks

like image 612
renanreismartins Avatar asked Sep 26 '26 03:09

renanreismartins


1 Answers

This may be the most straightforward / concise way to do it, and it doesn't require building any sets (which is relatively expensive).

(keys (select-keys map-1 (keys map-2)))

Finally, an optimized version, which is a bit faster, and a bit harder to read:

(keep #(some->> % (find map-1) key) (keys map-2))
like image 117
noisesmith Avatar answered Sep 27 '26 21:09

noisesmith