Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Applying multiple filters to a collection in a thrush in Clojure

Tags:

clojure

The following code

(let [coll [1 2 3 4 5]
      filters [#(> % 1) #(< % 5)]]
  (->> coll
       (filter (first filters))
       (filter (second filters))))

Gives me

(2 3 4)

Which is great, but how do I apply all the filters in coll without having to explicitly name them?

There may be totally better ways of doing this, but ideally I'd like to know an expression that can replace (filter (first filters)) (filter (second filters)) above.

Thanks!

like image 438
George Avatar asked Aug 03 '11 14:08

George


1 Answers

Clojure 1.3 has a new every-pred function, which you could use thusly:

(filter (apply every-pred filters) coll)
like image 148
Justin Kramer Avatar answered Sep 29 '22 11:09

Justin Kramer