emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Multidimensional list comprehension with binding


From: João Távora
Subject: Re: Multidimensional list comprehension with binding
Date: Mon, 11 Jun 2018 17:34:55 +0100

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 <address@hidden> 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

reply via email to

[Prev in Thread] Current Thread [Next in Thread]