I want to apply two (or more) predicates to a single value. For example, say I want to test if a value is a positive integer:
(defn posint? [n]
(and (integer? n) (pos? n)))
That does it, but what if I want to compose the predicates applied? I don't want to write a function for each possible combination of predicates.
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))
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
If you're looking to do these checks inline, the following may be what you are looking for.
(defn predicate-test [fns t]
"test all predicates against value t and return true iff all predicates return true."
(every? true? ((apply juxt fns) t)))
(predicate-test [integer? pos?] 4)
You could then create named versions for your most used predicate tests:
(def posint? (partial predicate-test [integer? pos?])
(posint? 4)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With