(merge-ordered-lists): Dot a few more is

Suggested by Mattias Engdegård.

* lisp/subr.el (merge-ordered-lists): Don't mutate the arg.

* test/lisp/subr-tests.el (subr-tests--merge-ordered-lists): Make the
test a bit more precise.
This commit is contained in:
Stefan Monnier 2023-11-16 09:50:45 -05:00
parent cf00f1526d
commit 44b5761b44
2 changed files with 7 additions and 4 deletions

View file

@ -2695,6 +2695,7 @@ By default we choose the head of the first list."
;; Algorithm inspired from
;; [C3](https://en.wikipedia.org/wiki/C3_linearization)
(let ((result '()))
(setq lists (remq nil lists)) ;Don't mutate the original `lists' argument.
(while (cdr (setq lists (delq nil lists)))
;; Try to find the next element of the result. This
;; is achieved by considering the first element of each

View file

@ -365,7 +365,7 @@
(defalias 'subr-tests--mode-C #'subr-tests--mode-B)
(derived-mode-add-parents 'subr-tests--mode-A '(subr-tests--mode-C))
(ert-deftest subt-tests--derived-mode-add-parents ()
(ert-deftest subr-tests--derived-mode-add-parents ()
;; The Right Answer is somewhat unclear in the presence of cycles,
;; but let's make sure we get tolerable answers.
;; FIXME: Currently `prog-mode' doesn't always end up at the end :-(
@ -381,12 +381,14 @@
'(subr-tests--mode-A subr-tests--mode-B prog-mode
subr-tests--mode-C subr-tests--derived-mode-1))))))
(ert-deftest subt-tests--merge-ordered-lists ()
(ert-deftest subr-tests--merge-ordered-lists ()
(should (equal (merge-ordered-lists
'((B A) (C A) (D B) (E D C)))
'((B A) (C A) (D B) (E D C))
(lambda (_) (error "cycle")))
'(E D B C A)))
(should (equal (merge-ordered-lists
'((E D C) (B A) (C A) (D B)))
'((E D C) (B A) (C A) (D B))
(lambda (_) (error "cycle")))
'(E D C B A)))
(should-error (merge-ordered-lists
'((E C D) (B A) (A C) (D B))