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: Joost Kremers
Subject: Re: iterating over a list while removing elements
Date: 19 Mar 2014 18:28:35 GMT
User-agent: slrn/pre1.0.0-18 (Linux)

Pascal J. Bourguignon wrote:
> (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)))

Or use --remove from the dash library. No need for lambda:

(require 'dash)

(defun multisearch-make-files-list (directory)
  "Return a list of files in DIRECTORY, with directory references
and directories removed."
  (--remove (or (multisearch-directory-ref-p it) ; or seems to better express 
the intention of the doc string.
                (file-directory-p it)
                (not (file-readable-p it)))
            (directory-files directory t)))

It's less portable, though, because dash doesn't come with Emacs.

Note, BTW, that file-directory-p returns t for "." and "..". It seems to
me that the only two names that directory-files could return that you
really want to exclude are those two,[1] so there's no need for
multisearch-directory-ref-p, I think. (Or is there?)

Joost



[1] Files can have dots in their names, so what do you want to do with a
file whose name ends in a dot? Or two? Unlikely, for sure, but not
impossible.

-- 
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


reply via email to

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