How do you compose two or more predicates in clojure?
If you want to compose them in an and
relationship, use every-pred
:
((every-pred pos? even?) 5)
;false
((every-pred pos? even?) 6)
;true
((every-pred pos? even?) -2)
;false
And if you want to compose them in an or
relationship, use some-fn
:
((some-fn pos? even?) 5)
;true
((some-fn pos? even?) 6)
;true
((some-fn pos? even?) -2)
;true
((some-fn pos? even?) -3)
;false
In Clojure 1.3 there is actually a built-in function called every-pred
to do just this. See here.
(defn posint? [n]
((every-pred integer? pos?) n))