Filtering maps based on key value pairs in Clojure
Code
(defn key-has-value? [key value map]
(= value (get map key)))
(defn map-filter [key value seq-of-maps]
(filter #(key-has-value? key value %) seq-of-maps))
(defn maps-having [filter-map seq-of-map]
(if (empty? filter-map)
seq-of-map
(let [[key value] (first filter-map)
filtered-seq (map-filter key value seq-of-map)]
(recur (rest filter-map) filtered-seq))))
;; let's test the above code
(def users [{"name" "Karthik", "country" "India"}
{"name" "Pari", "country" "India"}
{"name" "John", "country" "England"}])
(key-has-value? "country" "India" {"name" "Karthik", "country" "India"})
(map-filter "country" "India" users)
(maps-having {"country" "India" "name" "Karthik"} users)
Notes