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: Pascal J. Bourguignon
Subject: Re: iterating over a list while removing elements
Date: Wed, 19 Mar 2014 14:12:27 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

lee <lee@yun.yagibdah.de> writes:

> Hi,
>
> what is the defined behaviour when you iterate over a list and remove
> elements from that very list?  For example:
>
>
> (defsubst multisearch-directory-ref-p (dots)
>   "Return t when the string DOTS ends in a directory reference."
>   (or
>    (string-match "\\.$" dots)
>    (string-match "\\.\\.$" dots)))
>
> (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)))
>     (dolist (entry files-list files-list)
>       (unless (and
>              (not (multisearch-directory-ref-p entry))
>              (file-directory-p entry)
>              (file-readable-p entry))
>       (setq files-list (delete entry files-list))))))
>
>
> 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 ...


(require 'cl)

(defun multisearch-make-files-list (directory)
  "Return a list of files in DIRECTORY, with directory references
and directories removed."
  (remove-if (lambda (entry)
               (and (not (multisearch-directory-ref-p entry))
                    (file-directory-p entry)
                    (file-readable-p entry)))
              (directory-files directory t)))

However, your test conditions looks strange to me, compared to the
docstring.  In natural language, AND means OR, in general.

-- 
__Pascal Bourguignon__
http://www.informatimago.com/
"Le mercure monte ?  C'est le moment d'acheter !"


reply via email to

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