Function: member-if

member-if is a byte-compiled function defined in subr.el.gz.

Signature

(member-if PRED LIST)

Documentation

Non-nil if PRED is true for at least one element in LIST.

Returns the suffix of LIST starting with the first element that satisfies PRED, or nil if none do.

Compatibility note: this function replaces cl-member-if but does not support the latter's :key KEY-FN argument. It is better to compose any KEY-FN into PRED. For example, you can replace

    (cl-member-if #'foo items :key #'bar)

with

    (member-if (lambda (x) (foo (bar x))) items)

View in manual

Probably introduced at or before Emacs version 31.1.

Aliases

any

Source Code

;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun member-if (pred list)
  "Non-nil if PRED is true for at least one element in LIST.
Returns the suffix of LIST starting with the first element that
satisfies PRED, or nil if none do.

Compatibility note: this function replaces `cl-member-if' but does not
support the latter's `:key KEY-FN' argument.  It is better to compose
any KEY-FN into PRED.  For example, you can replace

    (cl-member-if #\\='foo items :key #\\='bar)

with

    (member-if (lambda (x) (foo (bar x))) items)"
  (declare (compiler-macro
            (lambda (form)
              (if (not (internal--effect-free-fun-arg-p pred))
                  ;; Don't inline since it would just duplicate the code
                  ;; without allowing any more optimizations.
                  form
                (let* ((x (make-symbol "x")))
                  `(drop-while (lambda (,x)
                                 (not (funcall ,pred ,x)))
                               ,list))))))
  (drop-while (lambda (x) (not (funcall pred x))) list))