unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "João Távora" <joaotavora@gmail.com>
To: Michael Brand <michael.ch.brand@gmail.com>
Cc: emacs-devel <emacs-devel@gnu.org>
Subject: Re: Multidimensional list comprehension with binding
Date: Mon, 11 Jun 2018 17:34:55 +0100	[thread overview]
Message-ID: <CALDnm51nBoOgQTgxLBXwebPKVGx2fLhoLyPzr080AWQLEHaYTQ@mail.gmail.com> (raw)
In-Reply-To: <CALn3zojMktT2eA4nVWGy5t+uM2p9Ae4dOKWkeKABgvm-3KMZ7A@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1697 bytes --]

Your loop is breaking early because res iterates in
a list of only one value.

You want

for res = (length arg)

to replace the second line, probably.

On Mon, Jun 11, 2018 at 5:25 PM, Michael Brand <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
>   return [[res, arg]
>           for arg in ["ab", "c", ""]
>           for res in [len(arg)]
>           if res % 2 == 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 (= (% 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))
>                        (= (% res 2) 0))
>              collect (list res arg)))
> #+end_src
> #+results:
> : ((2 "ab") (0 ""))
>
>


-- 
João Távora

[-- Attachment #2: Type: text/html, Size: 2502 bytes --]

  reply	other threads:[~2018-06-11 16:34 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-06-11 16:25 Multidimensional list comprehension with binding Michael Brand
2018-06-11 16:34 ` João Távora [this message]
2018-06-11 18:25   ` Michael Brand

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=CALDnm51nBoOgQTgxLBXwebPKVGx2fLhoLyPzr080AWQLEHaYTQ@mail.gmail.com \
    --to=joaotavora@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=michael.ch.brand@gmail.com \
    /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 public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).