Zach Beane has a nice Common Lisp tip about applying and. The problem that the tip addresses is how to apply and
to a list of values to test that they are all true. One might think that you could simply use apply
in the normal way
(apply #'and list)
but and
is a macro so this doesn’t work. The solution is to use the every
function like this
(every #'identity list)
This will return t
if every element of list is t
. All this is also true of Emacs Lisp so it’s worth mentioning in an Elisp context too.
Although Beane doesn’t mention it, the same problem exists for or
. The way to handle or
is with the some
function
(some #'identity list)
The some
function will return the first true element in the list or nil
if there is none.
Longtime readers will remember that we’ve talked about some before. Both some
and every
are a bit more general than I’ve indicated here. Their first argument can be any predicate so that
(some 'pred list)
will return the first element of the list for which pred
evaluates to t
. Similarly for every
. Also note that for Emacs Lisp it suffices to just quote the predicate function rather than using the #'
that Common Lisp requires.