1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2025-12-06 06:20:55 -08:00

Use take where clearly safe to do so (bug#56521)

* lisp/emacs-lisp/seq.el (seq-take):
* lisp/auth-source.el (auth-source-secrets-search)
(auth-source-plstore-search):
* lisp/gnus/message.el (message-insert-formatted-citation-line):
* lisp/net/dbus.el (dbus-unregister-object):
* lisp/replace.el (occur-context-lines):
* test/src/print-tests.el (print-circular): Replace hand-written loop
or `butlast` call with `take` for clarity, performance and validation.
We have the equivalence
(take N LIST) = (butlast LIST (- (length LIST) N)).
This commit is contained in:
Mattias Engdegård 2022-07-17 19:05:03 +02:00
parent 2d97fe2710
commit 5ad8f3e570
6 changed files with 14 additions and 16 deletions

View file

@ -587,11 +587,13 @@ Signal an error if SEQUENCE is empty."
(cl-defmethod seq-take ((list list) n)
"Optimized implementation of `seq-take' for lists."
(let ((result '()))
(while (and list (> n 0))
(setq n (1- n))
(push (pop list) result))
(nreverse result)))
(if (eval-when-compile (fboundp 'take))
(take n list)
(let ((result '()))
(while (and list (> n 0))
(setq n (1- n))
(push (pop list) result))
(nreverse result))))
(cl-defmethod seq-drop-while (pred (list list))
"Optimized implementation of `seq-drop-while' for lists."