vfclists · 2022-08-20 00:06
**Solved** https://www.reddit.com/r/emacs/comments/wst1cr/how_can_i_uses_clloop_to_collect_values_from/il17k1i/
I found this code on emacs stackexchange showing how `cl-loop` is used to gather information from an org-table
#+NAME: my-filter
#+BEGIN_SRC elisp :var tbl=table1 val="USA" :colnames y
(cl-loop for row in tbl
if (equal (nth 3 row) val)
collect row into newtbl
finally return newtbl)
#+END_SRC
I tried something similar with cl-loop but failed terribly, so I switch back to my trusty `dolist`.
#+BEGIN_SRC elisp :var tbl=ParedTable val="." :results output raw
(let ((somevar '()))
(dolist (row tbl)
(add-to-list 'somevar (append (list (nth 2 row)) (nth 0 row))))
somevar)
#+END_SRC
The code creates an `alist` from some columns of a table.
How can I used the `cl-loop` to accomplish the same thing?
ieure · 2022-08-20 00:39
If you want people to tell you what's wrong with some code, you need to share it. Nobody can tell you what's wrong with code that can't be seen.
Also helpful: a description of the way in which it doesn't work, and the way you want it to work.
flyin1501 · 2022-08-20 04:58
- Do not use `add-to-list` for local variables.
Variables can be dynamically bound (local variables are basically temporarily bound global variables) or lexically bound (the enclosing function or `let` has its own local association between variables and values). Using lexical binding makes your code less confusing for other people and provides better performance.
Instead of `(add-to-list 'symbol element)`, use `(push variable element)`.
- `(cl-loop for x in list do body)` is equivalent to `(dolist (x list) body)`.
- `(cl-loop for x in list collect body)` is equivalent to `(let ((result nil)) (dolist (x list) (push body result)) (nreverse result))`.
- `append` should not be used just to create a cons cell. You're wasting CPU cycles with that extra call to `list`. Use `(cons x y)` instead.
- Try using emacs-lisp-macroexpand on the cl-loop to see what it's expanding into.
vfclists · 2022-08-20 05:25
I eventually got it to work by some fancy (to me) technique I'm yet to properly understand.
#+BEGIN_SRC elisp :var tbl=ParedTable val="."
(cl-loop for row in tbl
collect `(,(nth 0 row) ,(nth 2 row))
)
#+END_SRC
vfclists · 2022-08-20 16:13
Managed to fix it:
https://www.reddit.com/r/emacs/comments/wst1cr/how_can_i_uses_clloop_to_collect_values_from/il17k1i/
ieure · 2022-08-20 16:44
You're using a [backquote](https://www.gnu.org/software/emacs/manual/html_node/elisp/Backquote.html), also called a quasiquote. The backquote character begins an unevaluated expression, same as a single quote, except `,thing` will eval `thing` and include it in the list. So your code:
`(,(nth 0 row) ,(nth 2 row))
Is equivalent to:
(list (nth 0 row) (nth 2 row))
It constructs a list of two elements, which are extracted from `row`.
P.S. Lisp convention is to put all the closing parens on the same line, like this:
(cl-loop for row in tbl
collect `(,(nth 0 row) ,(nth 2 row)))
And the code is short enough that, personally, I'd put it all on one line.
vfclists · 2022-08-20 17:01
Would it be fair to say that `quote ` does not allow anything in the list to be evaluated where as `quasiquote` allows items in the list to be evaluated if they are preceded with a `,`?
>P.S. Lisp convention is to put all the closing parens on the same line
I'm aware of that convention but I only apply it when I've got the whole block of code working to my satisfaction and understand how it is all put together
ieure · 2022-08-20 17:53
> Would it be fair to say that quote does not allow anything in the list to be evaluated where as quasiquote allows items in the list to be evaluated if they are preceded with a ,?
Yes. Quoting inhibits evaluation, period. Backquoting allows you to control which elements of the list are or are not evaluated.
deaddyfreddy · 2022-08-22 17:36
What's the reason to use `cl-loop` here? `mapcar` should work fine, and it's much easier to understand.
vfclists · 2022-08-22 21:21
As a relative Lisp newbie the keywords **loop** and **for** are so familiar from my experience with other languages.
They give me such a warm feeling of familiarity.
deaddyfreddy · 2022-08-22 21:55
it's just an illusion of familiarity