Search code examples
clojure

How to filter a persistent map in Clojure?


I have a persistent map which I want to filter. Something like this:

(filter #(-> % val (= 1)) {:a 1 :b 1 :c 2})

The above comes out as ([:a 1] [:b 1]) (a lazy sequence of map entries). However I want to be get {:a 1 :b 1}.

How can I filter a map so it remains a map without having to rebuild it from a sequence of map entries?


Solution

  • And another one:

    (let [m {:a 1 :b 2 :c 1}]
      (select-keys m (for [[k v] m :when (= v 1)] k)))