clojure - Filter a collection by multiple attributes provided with a map -
i have collection want filter. filter done using map key attribute in collection item, , value collection items should match. example:
(let [filters {:name "test"                :type "new"}       collection [{:name "testable"  :type "old"}                   {:name "shoudwork" :type "new"}                   {:name "testable"  :type "new"}]]) right now, have built filter function take single attribute , value. want expand able take filter hashmap.
this current filter looks like:
(filter #(re-find (re-pattern "test") (string/lower-case (% :name))) collection) in other words, want filter not take "test" hardcoded, should value filters let binding, , (% :name) not hardcoded :name, key filters let binding.
create function returns filtering function single [key expected-value]:
(defn regex-value [[k expected-rex]]   (fn [c] (re-find (re-pattern expected-rex)                     (clojure.string/lower-case (get c k))))) create function create compounded filtering condition:
(defn build-filter [filters]   (apply every-pred (map regex-value filters))) use it:
(let [filters {:name "test"                :type "new"}       collection [{:name "testable"  :type "old"}                   {:name "shoudwork" :type "new"}                   {:name "testable"  :type "new"}]]   (filter (build-filter filters) collection)) 
Comments
Post a Comment