help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [External] : Re: Appending lists


From: Stefan Monnier
Subject: Re: [External] : Re: Appending lists
Date: Sun, 20 Jun 2021 18:09:52 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

>   ;; (setq completion-ignored-extensions
>   ;;       (nconc completion-ignored-extensions '(".bcf" ".elc" ".run.xml")) )
> can be changed into (should be changed, since the above makes
> no difference - ?) it can be changed into
>   (nconc completion-ignored-extensions '(".bcf" ".run.xml"))

That will fail to do what you want if `completion-ignored-extensions`
was empty just before executing the code.
And it will have undesirable side effects if
`completion-ignored-extensions` happened to hold a list some parts of
which are also used elsewhere, e.g. if it was by a code such as

    (setq completion-ignored-extensions '("some" "constant" "list"))

Since you probably don't perform this operation inside a tight loop
where performance could be significant I'd recommend you just use
`append` instead.

>   ;; (let ((modes (list
>   ;;               '("\\.bal\\'"     . balance-mode)
>   ;;               '("\\.grm\\'"     . sml-mode)
>   ;;               '("\\.lu\\'"      . lua-mode)
>   ;;               '("\\.nqp\\'"     . perl-mode)
>   ;;               '("\\.php\\'"     . html-mode)
>   ;;               '("\\.pic\\'"     . nroff-mode)
>   ;;               '("\\.pl\\'"      . prolog-mode)
>   ;;               '("\\.sed\\'"     . conf-mode)
>   ;;               '("\\.service\\'" . conf-mode)
>   ;;               '("\\.tex\\'"     . latex-mode)
>   ;;               '("\\.xr\\'"      . conf-xdefaults-mode)
>   ;;               '("*"             . text-mode) )))
>   ;;   (setq auto-mode-alist (nconc modes auto-mode-alist)) )
>
> might as well be
>
>   (nconc (list
>           '("\\.bal\\'"     . balance-mode)
>           '("\\.grm\\'"     . sml-mode)
>           '("\\.lu\\'"      . lua-mode)
>           '("\\.nqp\\'"     . perl-mode)
>           '("\\.php\\'"     . html-mode)
>           '("\\.pic\\'"     . nroff-mode)
>           '("\\.pl\\'"      . prolog-mode)
>           '("\\.sed\\'"     . conf-mode)
>           '("\\.service\\'" . conf-mode)
>           '("\\.tex\\'"     . latex-mode)
>           '("\\.xr\\'"      . conf-xdefaults-mode)
>           '("*"             . text-mode) )
>          auto-mode-alist)

Here `nconc` is a safe replace for `append` because indeed the first arg
is a list you just constructed by `list` so you know for sure it's not
shared with anything else.

But the second above code is just an expensive no-op because your
`nconc` will modify its first argument and not its second, so it will
construct a new list, will add the content of `auto-mode-alist` to its
end and then throw away the result.

Here's another way you could write the code:

    (setq auto-mode-alist 
          `(("\\.bal\\'"     . balance-mode)
            ("\\.grm\\'"     . sml-mode)
            ("\\.lu\\'"      . lua-mode)
            ("\\.nqp\\'"     . perl-mode)
            ("\\.php\\'"     . html-mode)
            ("\\.pic\\'"     . nroff-mode)
            ("\\.pl\\'"      . prolog-mode)
            ("\\.sed\\'"     . conf-mode)
            ("\\.service\\'" . conf-mode)
            ("\\.tex\\'"     . latex-mode)
            ("\\.xr\\'"      . conf-xdefaults-mode)
            ("*"             . text-mode)
            ,@auto-mode-alist))


-- Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]