From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: lee Newsgroups: gmane.emacs.help Subject: Re: iterating over a list while removing elements Date: Thu, 20 Mar 2014 17:33:50 +0100 Organization: my virtual residence Message-ID: <8738icx21d.fsf@yun.yagibdah.de> References: <87ioral4ck.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1395333260 29311 80.91.229.3 (20 Mar 2014 16:34:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 20 Mar 2014 16:34:20 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Mar 20 17:34:30 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WQfvJ-0006TT-UB for geh-help-gnu-emacs@m.gmane.org; Thu, 20 Mar 2014 17:34:30 +0100 Original-Received: from localhost ([::1]:48214 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WQfvJ-0001F9-C9 for geh-help-gnu-emacs@m.gmane.org; Thu, 20 Mar 2014 12:34:29 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:47435) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WQfut-0000wm-W8 for help-gnu-emacs@gnu.org; Thu, 20 Mar 2014 12:34:08 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WQfup-0002Yd-01 for help-gnu-emacs@gnu.org; Thu, 20 Mar 2014 12:34:03 -0400 Original-Received: from client-194-42-186-216.muenet.net ([194.42.186.216]:41364 helo=yun.yagibdah.de) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WQfuo-0002XQ-MS for help-gnu-emacs@gnu.org; Thu, 20 Mar 2014 12:33:58 -0400 Original-Received: from lee by yun.yagibdah.de with local (Exim 4.80.1) (envelope-from ) id 1WQfum-0003OP-Ln for help-gnu-emacs@gnu.org; Thu, 20 Mar 2014 17:33:56 +0100 In-Reply-To: <87ioral4ck.fsf@kuiper.lan.informatimago.com> (Pascal J. Bourguignon's message of "Wed, 19 Mar 2014 14:12:27 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) Mail-Followup-To: help-gnu-emacs@gnu.org X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 194.42.186.216 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:96645 Archived-At: "Pascal J. Bourguignon" writes: > lee 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.