If I have a boolean function f(x) = {print(x); return is_even(x)}
that does some time-consuming (in this example, for illustrative purposes, side-effect-producing) stuff and a list a={2,3,4}
I can check if f()
is true for every element of a
with
apply(And, map(f, a))
but that wastefully applies f
to every element of a
instead of returning False
after the second element. This does the right thing
And(f(2), f(3), f(4))
but of course I want to dynamically create the list. Lisp solves this problem with the built-in "every
" function (and similarly for "some
") [originally I called them macros]. How do you do it in other languages? Eg,
every(f, a)
should return False
without evaluating f(4)
. (And similarly for some
.)