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

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

Re: iterating over a list while removing elements


From: Stefan
Subject: Re: iterating over a list while removing elements
Date: Wed, 19 Mar 2014 08:39:39 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

>    (string-match "\\.$" dots)
>    (string-match "\\.\\.$" dots)))

You meant

    (string-match "\\.\\'" dots)
    (string-match "\\.\\.\\'" dots)))

> Surprisingly, this /appears/ to work.  Can I take that for granted, or
> is this a stupid thing to do?  It`s like someone pulling the chair
> you`re about to sit on from underneath you ...

This is undocumented, so better not rely on the details of the behavior.
You can rely on the fact that dolist will behave sanely, tho: it should
not go berzerk, it should go through at least all elements still
remaining in the list, and at most all elements that have been in
the list.

But you should better not assume that dolist will skip the "entry" you
just removed.  E.g. you could do the following, which should be somewhat
faster (since `delete' is O(n)):

(defun multisearch-make-files-list (directory)
  "Return a list of files in DIRECTORY, with directory references
and directories removed."
  (let ((files-list (directory-files directory t))
        (newlist '()))
    (dolist (entry files-list (nreverse newlist))
      (when (and
               (not (multisearch-directory-ref-p entry))
               (file-directory-p entry)
               (file-readable-p entry))
        (push entry newlist)))))



        Stefan




reply via email to

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