I have this Clojure code:
(defn apply-all-to-arg [& s]
(let [arg (first s)
exprs (rest s)]
(for [condition exprs] (condition arg))))
(defn true-to-all? [& s]
(every? true? (apply-all-to-arg s)))
This is test code:
(apply-all-to-arg 2 integer? odd? even?)
=> (true false true)
(every? true? (apply-all-to-arg 2 integer? odd? even?)
=> false
(true-to-all? 2 integer? odd? even?)
=> true
My question is:
Why does the function true-to-all? return true (it must have returned false instead)
true-to-all?callsapply-all-to-argwith the single arguments. So you’re not calling(every? true? (apply-all-to-arg 2 integer? odd? even?), but rather:So in
apply-all-to-argthe value ofargwill be that list and the value ofexprswill be the empty list. Sinceevery?will be true for the empty list no matter what the condition is, you’ll get back true.To fix this you can either change
apply-all-to-arg, so that it accepts a list instead of a variable number of arguments, or you can changetrue-to-all?, so that it passes the contents ofsas multiple arguments rather than a single list (by usingapply).