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: lee
Subject: Re: iterating over a list while removing elements
Date: Thu, 20 Mar 2014 17:33:50 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

"Pascal J. Bourguignon" <address@hidden> writes:

> lee <address@hidden> 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)))

Interesting :)  Looking at the docstring for `remove-if', I wonder how
many copies this makes?  ... Hm, does that even work?  How and why would
each list member be fed to the lambda thing?  And the lambda thing would
return either nil or t, which is not what I would want to remove or not
...  And no members of the list are nil: So would that remove anything
at all?

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

Good catch --- I found my function returned the directory names instead
of the file names, that was a bug ...


(defun multisearch-make-files-list (directory &optional match)
  "Return a list of files in DIRECTORY, with directory references and
directories removed.

When MATCH is non-nil, only files that match the regexp MATCH are
included in the list."
  (let ((files-list (directory-files directory t match t))
        (clean-list nil))
    (dolist (entry files-list clean-list)
      (when (and
             (not (multisearch-directory-ref-p entry))
             (not (file-directory-p entry))
             (file-readable-p entry))
        (setq clean-list (cons entry clean-list))))))


see https://github.com/lee-/emacs/tree/master/multisearch


-- 
Knowledge is volatile and fluid.  Software is power.



reply via email to

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