unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Multidimensional list comprehension with binding
@ 2018-06-11 16:25 Michael Brand
  2018-06-11 16:34 ` João Távora
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Brand @ 2018-06-11 16:25 UTC (permalink / raw)
  To: emacs-devel

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 ""))



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-06-11 18:25 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-06-11 16:25 Multidimensional list comprehension with binding Michael Brand
2018-06-11 16:34 ` João Távora
2018-06-11 18:25   ` Michael Brand

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).