Function: take-while
take-while is a byte-compiled function defined in subr.el.gz.
Signature
(take-while PRED LIST)
Documentation
Return the longest prefix of LIST whose elements satisfy PRED.
Other relevant functions are documented in the list group.
Probably introduced at or before Emacs version 31.1.
Shortdoc
;; list
(take-while #'numberp '(1 2 three 4 five))
=> (1 2)
Source Code
;; Defined in /usr/src/emacs/lisp/subr.el.gz
(defun take-while (pred list)
"Return the longest prefix of LIST whose elements satisfy PRED."
(declare (compiler-macro
(lambda (form)
(let* ((tail (make-symbol "tail"))
(r (make-symbol "r")))
(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 ((,r nil)
(,tail ,list))
(while (and ,tail (funcall ,pred (car ,tail)))
(push (car ,tail) ,r)
(setq ,tail (cdr ,tail)))
(nreverse ,r)))))))
(let ((r nil))
(while (and list (funcall pred (car list)))
(push (car list) r)
(setq list (cdr list)))
(nreverse r)))