Search code examples
clojure

How to apply every of many predicate functions (`and` them)


I have many predicate functions in a vector fs, and would like to and them all together. This works for the first two functions:

((and (first fs) (second fs)) value)

However I would like to write code that applies and to all the functions, no matter how many there are. (apply and fs) does not compile because and is a macro.

For an example of predicate functions working with and, try this:

((and number? integer?) 1)

Edit In case you don't read the comments, both and constructions above are BAD EXAMPLES. For same constructions to actually work use every-pred rather than and.


Solution

  • Since you are working with predicates, every-pred may be of interest.

    After reading your edit - thanks, I didn't know and you could do that with and. Additionally I've edited my answer, and I think you definitely want to apply every-pred

    ((apply every-pred [number? integer?]) 1)
    => true
    ((apply every-pred [number? integer?]) "asdf")
    => false