From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: =?UTF-8?B?Sm/Do28gVMOhdm9yYQ==?= Newsgroups: gmane.emacs.devel Subject: Re: Multidimensional list comprehension with binding Date: Mon, 11 Jun 2018 17:34:55 +0100 Message-ID: References: NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="0000000000009ed0ec056e605536" X-Trace: blaine.gmane.org 1528734846 28141 195.159.176.226 (11 Jun 2018 16:34:06 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 11 Jun 2018 16:34:06 +0000 (UTC) Cc: emacs-devel To: Michael Brand Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jun 11 18:34:01 2018 Return-path: Envelope-to: ged-emacs-devel@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 1fSPlY-00077k-7y for ged-emacs-devel@m.gmane.org; Mon, 11 Jun 2018 18:34:00 +0200 Original-Received: from localhost ([::1]:50146 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fSPnd-00022g-FO for ged-emacs-devel@m.gmane.org; Mon, 11 Jun 2018 12:36:09 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:42236) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1fSPmo-00021e-J0 for emacs-devel@gnu.org; Mon, 11 Jun 2018 12:35:19 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1fSPmn-0004tg-7W for emacs-devel@gnu.org; Mon, 11 Jun 2018 12:35:18 -0400 Original-Received: from mail-io0-x234.google.com ([2607:f8b0:4001:c06::234]:39169) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1fSPmm-0004sl-N7 for emacs-devel@gnu.org; Mon, 11 Jun 2018 12:35:17 -0400 Original-Received: by mail-io0-x234.google.com with SMTP id f1-v6so24605998ioh.6 for ; Mon, 11 Jun 2018 09:35:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=9TbsWkpkxNjhjL9WEVzVhM+s0K5TX6+afndnmqtrR3A=; b=HcUXiFpt5ZyjYRYsaCMeHirIfAQ2nxu4UW6m40IetJPbLAzb2EQaT/lDeyr2l6Uruh mgyg/dDZOfKSj4gZZS1yuf6NuBpqNHm93ZKbRUr+1dd2rubI2x5pM88MifcOeV88vJum XrHDmo5cUAKlvVE254iuTzvoMoEYhA2fNztIOYfGi9HCtoSVwV8Qg+qpuZzJMvjNMgHr gX4+SA3VUtMxeA4B4S0kpNie+icI93eQIjO1mL9ufce8h0WFj0qy5bPaN/ugioRhWjEc /lFF+fAj8kUEC7UfuOo18qyBOmbbGbG/dY88BOYLl2WyQJVfDw+6E9np6+V8XFhpbsw9 Sp8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=9TbsWkpkxNjhjL9WEVzVhM+s0K5TX6+afndnmqtrR3A=; b=WLSupxHlHfZyWKLVYNRRVRrY3ACaXpTIR5ws5FYhV5cq9+dia1F6UMfMHOtWNSIF6G FH7jhZhtx7d8wh+YRWlk+SsrnZMzGv8AVjiwF9Xy5dyeZfrFZyfVKjit3g9P0I3PBajb CAIWptD6F4nWh63dfPhzViWmueBxIViCkAExfS9t0gGs4kXwTMCM0Ru/nPguQPkwIyDH iWIwFkEH927fjVt+7JDYNPyT5+R2Z7puHP2qoLDNorhY2fpJPT8nPIDA0k//PbYRCEpR 2L1ggUNUYzlEZinkYtKFnpz+87/SqxaU6O19LLfkt1WbvqmaR/wZ3nouosLQSG+sIdXj Qcbw== X-Gm-Message-State: APt69E03HgldCADgfr2UagiEf3M9R7Y+XsbG8KNtJ6r7CeydskJ8hFSe vpzQM/vOZ4HPZvzLZivw0ZDHcwrV6xZi3VX8qmQ= X-Google-Smtp-Source: ADUXVKJPr0+sNOfPxfXueVFnMOs3AK0JwkozpykJB48eOVQoTafOb45SFXgHCpUGWm22XU7Izjh1BmqnfAOMtB5/YvA= X-Received: by 2002:a6b:9041:: with SMTP id s62-v6mr14302130iod.231.1528734915935; Mon, 11 Jun 2018 09:35:15 -0700 (PDT) Original-Received: by 2002:a4f:2246:0:0:0:0:0 with HTTP; Mon, 11 Jun 2018 09:34:55 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4001:c06::234 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:226227 Archived-At: --0000000000009ed0ec056e605536 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Your loop is breaking early because res iterates in a list of only one value. You want for res =3D (length arg) to replace the second line, probably. On Mon, Jun 11, 2018 at 5:25 PM, Michael Brand wrote: > Hi all > > I try to replicate the following Python list comprehension with Emacs > Lisp. It reports the list elements with its length when the length is > even. The expectation is to calculate the length only once and to not > use a second and nested list comprehension: > > #+begin_src python :results verbatim > return [[res, arg] > for arg in ["ab", "c", ""] > for res in [len(arg)] > if res % 2 =3D=3D 0] > #+end_src > #+results: > : [[2, 'ab'], [0, '']] > > The above result is as expected (Python 2.7 and 3.6). > > In my trial with Emacs Lisp the syntax for the equivalent expression > seems to be accepted but the result is not the expected ((2 "ab") (0 > "")): > > #+begin_src emacs-lisp :results verbatim > (cl-loop for arg in '("ab" "c" "") > for res in (list (length arg)) > if (=3D (% res 2) 0) > collect (list res arg)) > #+end_src > #+results: > : ((0 "ab")) > > Am I doing something wrong or is this a bug in > emacs-26.1-2189-g8377ca6 and 26.1? > > Michael > > PS: A workaround would be this less elegant version: > > #+begin_src emacs-lisp :results verbatim > (let (res) > (cl-loop for arg in '("ab" "c" "") > if (progn (setq res (length arg)) > (=3D (% res 2) 0)) > collect (list res arg))) > #+end_src > #+results: > : ((2 "ab") (0 "")) > > --=20 Jo=C3=A3o T=C3=A1vora --0000000000009ed0ec056e605536 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Your loop is breaking early because res iterates in=C2=A0<= div>a list of only one value.

You want=C2=A0
=

for res =3D (length arg)=C2=A0

to replace the second line, probably.

On Mon, Jun 11, 2018 at 5:25 PM, Michael B= rand <michael.ch.brand@gmail.com> wrote:
Hi all

I try to replicate the following Python list comprehension with Emacs
Lisp. It reports the list elements with its length when the length is
even. The expectation is to calculate the length only once and to not
use a second and nested list comprehension:

#+begin_src python :results verbatim
=C2=A0 return [[res, arg]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for arg in ["ab", "c"= ;, ""]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 for res in [len(arg)]
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if res % 2 =3D=3D 0]
#+end_src
#+results:
: [[2, 'ab'], [0, '']]

The above result is as expected (Python 2.7 and 3.6).

In my trial with Emacs Lisp the syntax for the equivalent expression
seems to be accepted but the result is not the expected ((2 "ab")= (0
"")):

#+begin_src emacs-lisp :results verbatim
=C2=A0 (cl-loop for arg in '("ab" "c" "")=
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0for res in (list (length arg))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (=3D (% res 2) 0)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0collect (list res arg))
#+end_src
#+results:
: ((0 "ab"))

Am I doing something wrong or is this a bug in
emacs-26.1-2189-g8377ca6 and 26.1?

Michael

PS: A workaround would be this less elegant version:

#+begin_src emacs-lisp :results verbatim
=C2=A0 (let (res)
=C2=A0 =C2=A0 (cl-loop for arg in '("ab" "c" "= ")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (progn (setq res (length= arg))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0(=3D (% res 2) 0))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0collect (list res arg))) #+end_src
#+results:
: ((2 "ab") (0 ""))




--
Jo=C3=A3o T=C3=A1vo= ra
--0000000000009ed0ec056e605536--