all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: iterating over a list while removing elements
Date: Wed, 19 Mar 2014 14:12:27 +0100	[thread overview]
Message-ID: <87ioral4ck.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.17526.1395229207.10748.help-gnu-emacs@gnu.org

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	other threads:[~2014-03-19 13:12 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <mailman.17526.1395229207.10748.help-gnu-emacs@gnu.org>
2014-03-19 13:12 ` Pascal J. Bourguignon [this message]
2014-03-19 18:28   ` iterating over a list while removing elements Joost Kremers
2014-03-20 17:34     ` lee
2014-03-20 20:16       ` Eli Zaretskii
2014-03-21  5:25         ` lee
2014-03-20 16:33   ` lee
2014-03-19 11:39 lee
2014-03-19 12:39 ` Stefan
2014-03-20 16:02   ` lee
2014-03-19 13:11 ` Michael Albinus
2014-03-20 16:10   ` lee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ioral4ck.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.