From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Drew Adams Newsgroups: gmane.emacs.help Subject: RE: return first element in list with certain property Date: Mon, 20 Nov 2017 14:59:19 -0800 (PST) Message-ID: <520f070f-331d-4cf9-ad17-0294f178171f@default> References: <8660a60zjn.fsf@zoho.com> <87mv3gzndx.fsf@ericabrahamsen.net> <86ine4y7jy.fsf@zoho.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable X-Trace: blaine.gmane.org 1511218792 10281 195.159.176.226 (20 Nov 2017 22:59:52 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 20 Nov 2017 22:59:52 +0000 (UTC) To: Emanuel Berg , help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Mon Nov 20 23:59:48 2017 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1eGv2Z-0002LP-5a for geh-help-gnu-emacs@m.gmane.org; Mon, 20 Nov 2017 23:59:47 +0100 Original-Received: from localhost ([::1]:60137 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eGv2g-0002qC-Kb for geh-help-gnu-emacs@m.gmane.org; Mon, 20 Nov 2017 17:59:54 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:37716) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1eGv2H-0002q5-IC for help-gnu-emacs@gnu.org; Mon, 20 Nov 2017 17:59:30 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1eGv2C-00032S-IY for help-gnu-emacs@gnu.org; Mon, 20 Nov 2017 17:59:29 -0500 Original-Received: from userp1040.oracle.com ([156.151.31.81]:17048) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1eGv2C-00032D-9K for help-gnu-emacs@gnu.org; Mon, 20 Nov 2017 17:59:24 -0500 Original-Received: from aserv0021.oracle.com (aserv0021.oracle.com [141.146.126.233]) by userp1040.oracle.com (Sentrion-MTA-4.3.2/Sentrion-MTA-4.3.2) with ESMTP id vAKMxLIa001282 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 20 Nov 2017 22:59:22 GMT Original-Received: from aserv0122.oracle.com (aserv0122.oracle.com [141.146.126.236]) by aserv0021.oracle.com (8.14.4/8.14.4) with ESMTP id vAKMxLVo008731 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 20 Nov 2017 22:59:21 GMT Original-Received: from abhmp0016.oracle.com (abhmp0016.oracle.com [141.146.116.22]) by aserv0122.oracle.com (8.14.4/8.14.4) with ESMTP id vAKMxLAm010873; Mon, 20 Nov 2017 22:59:21 GMT In-Reply-To: <86ine4y7jy.fsf@zoho.com> X-Priority: 3 X-Mailer: Oracle Beehive Extensions for Outlook 2.0.1.9.1 (1003210) [OL 16.0.4615.0 (x86)] X-Source-IP: aserv0021.oracle.com [141.146.126.233] X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.4.x-2.6.x [generic] [fuzzy] X-Received-From: 156.151.31.81 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 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" Xref: news.gmane.org gmane.emacs.help:115017 Archived-At: > However the below tests seem to indicate no > problems for any of the solutions suggested > so far? Certainly there will be no problem with any of them when the list is small. > (let ((test-list '(0 1 2 3 4 5))) >=20 > (cl-dolist (e test-list) > (message "cl-dolist processing: %s" e) > (when (> e 1) (cl-return e) )) >=20 > (cl-find-if (lambda (e) (and (message "cl-find-if processing: %s" e) > (> e 1) )) > test-list) >=20 > (seq-find (lambda (e) (and (message "find-seq processing: %s" e) > (> e 1) )) > test-list) > ) > ;; cl-dolist processing: 0 > ;; cl-dolist processing: 1 > ;; cl-dolist processing: 2 > ;; cl-find-if processing: 0 > ;; cl-find-if processing: 1 > ;; cl-find-if processing: 2 > ;; find-seq processing: 0 > ;; find-seq processing: 1 > ;; find-seq processing: 2 As I said, `cl-find-if' apparently traverses the list twice. You see only one message for each element tested because your predicate is called only once per element. `cl-find-if' is implemented by first calling `cl-position', which uses your predicate. And then, once a satisfactory element is found, it calls `elt' to traverse the list again, to obtain that element. The call to `elt' does not use your predicate, so you see no messages for it. As I said, I have not bothered to profile the different suggestions, but a guess is that with a very large list `cl-find-if' will be slower.