some and every | ![]() |
(std-every <pred> <lst>) ; T if pred is non-nil for every element
(std-every-n <pred> <lists>) ; for predicates taking more args
(std-some <pred> <lst>) ; T if pred is non-nil for some elements
(std-some-n <pred> <lists>) ; for predicates taking more args
SOME
and EVERY
for AutoLISP backwards compatibility.
SOME
and EVERY
are basic boolean lisp functions, used to check a list for matching or not-matching predicates. They only return T
or NIL
.
The VL versions take multiple arguments, so we divide them into an one-arg version and a n-args version for AutoLISP.
These are the optimized versions, the simple definition would just be
some: (apply 'or (mapcar 'pred lst))
every: (apply 'and (mapcar 'pred lst))
Our versions break out of the checking loop on the first predicate which fails the test, STD-EVERY
stops iterating at the first NIL, STD-SOME
stops at the first non-nil return value.
STD-EVERY
returns T if the predicate returns non-nil for every element or NIL
if some elements return NIL
when applied to the predicate.
STD-SOME
returns T if the predicate returns non-nil for some elements or NIL
if every element returns NIL when applied to the predicate.
STD-EVERY
is mostly used as "insurance" to check if the arguments really satisfy the correct type, otherwise a user-defined warning or error can be signalled, or a default value can be restored. STD-SOME
is the logic counterpart, but not used that often.
Note:
If with the -N
versions (multiple lists for an n-ary predicate) any list in lists is longer than another, the remaining elements in the shorter lists default to NIL
. The predicate is applied as often as there are elements left in the longest list.
The unary versions std-some/std-every
allow an empty list to be used.
(std-some 'null nil) => nil
But the n-ary versions std-some-n/std-every-n
will fail if no lists are provided.
(std-some-n
'null
nil)
=> Error: too few arguments
;;; Check if all x and y values of points are non-nil
(std-every-n 'and (list (mapcar 'x-of points) (mapcar 'y-of points)))
;;; also:
(and (std-every 'numberp (mapcar 'x-of points) (std-every 'numberp (mapcar 'y-of points))
;;; or even simplier:
(std-every 'std-pointp points)
;;; ensure the whole list contains numbers
(if (not (std-every 'numberp nums)) (setq nums default-nums))
;;; or check if there's a zero in the list
(if (std-some 'zerop nums)) (setq nums default-nums)) (if (std-some 'null values) (std-warn "invalid empty element in values: ..."))
pred: a function or lambda list accepting one or n arguments.
lst: any list
lists: a list of lists. NIL
will throw the error: "too few arguments"
T or NIL.
The predicate is applied to each element in the list(s).
If the predicate has sideeffects they appear (length lst)
or (apply
'max
(mapcar
'length
lists))
times.
(std-require 'STDLIST)
Defined in STDLIST