Questions tagged [iteration]
28 questions
11
votes
2 answers
How to break out of a dolist loop?
Is it possible to break out of a dolist loop? if not, are there alternatives to dolist that don't involve a while loop and indexing a list?

ideasman42
- 8,375
- 1
- 28
- 105
6
votes
1 answer
How do I properly populate a function by iterating over an alist?
I've defined a function that is supposed to iterate over an alist, and use the CAR and CDR of each element in the list to populate the regexp in re-search-forward and the replacement string in replace-match, respectively. Here is what I have:
(defun…

rjww
- 125
- 9
4
votes
2 answers
How do I splat the arguments to the `or` function?
I would like to evaluate a list of booleans like
(nil nil t)
into a single boolean, such that if any element is true, then the expression evaluates to true.
I first looked at the (or) function in elisp, but it does not take a list of booleans as an…

Linus Arver
- 152
- 1
- 6
4
votes
3 answers
idiomatic way to lexically scope variables in a cl-loop body
Here is some asynchronous code in a cl-loop:
;;; foo.el --- -*- lexical-binding: t; -*-
(let ((my-list '(a b c)))
(cl-loop for index below (length my-list)
for value = (seq-elt my-list index)
do…

Damien Cassou
- 877
- 4
- 14
4
votes
2 answers
How to loop over every line in a buffer?
I am searching for some kind of reduce function, which loops through every line of a buffer, but I can not find it. Does anybody know the name of such a function?
Something similar to Perl
while (<>) {
...
}
Or a dolist for buffer lines.

ceving
- 1,308
- 1
- 14
- 28
3
votes
2 answers
Simple recursive function freezes Emacs — how to correct the definition?
I was trying to familiarize myself with several languages by doing the advent of code exercises in these languages, and one that got me stuck is Emacs Lisp, in the very first exercise.
The goal is to count, given a certain list of numbers, the…

jthulhu
- 205
- 1
- 8
3
votes
1 answer
How to distinguish input from the return value of body in while-no-input?
(while-no-input &rest BODY)
Execute BODY only as long as there's no pending input.
If input arrives, that ends the execution of BODY, and while-no-input
returns t. Quitting makes it return nil. If BODY finishes,
while-no-input returns whatever…

tejasvi88
- 151
- 5
3
votes
1 answer
How to map or iterate over a list of files and set the result to `org-agenda-files`?
looking for a little help with my function. I'm trying to create a loop-while that'll return a list of files back to org-agenda-files. It's just not working though and i keep getting errors. Any ideas how I can get this to work? Any using a…

Nick Martin
- 80
- 4
3
votes
0 answers
What kind of destructuring does cl-loop support?
1. Summary
Is it possible to destructure an unordered plist in cl-loop's for-clauses?
2. Details
From the documentation I would have expected destructuring to be universal, e.g. I would have expected
for (a . b) = data
and
for (a &rest b) =…

kdb
- 1,561
- 12
- 21
3
votes
4 answers
How to mapcar uneven lists?
I am aware I can use mapcar to join 2 lists into pairs, however all the map functions seem to quit at the shortest list.
This for example
(cl-mapcar #'concat '("1" "2" "3" "4") '("a" "b" "c"))
returns
("1a" "2b" "3c")
I basically want the same…

Oly
- 583
- 1
- 5
- 15
2
votes
0 answers
why does the Iterator Yield A Different Value?
I tested two similar generators, and the result confused me.
This iterator yields values as I expected:
ELISP> (iter-defun f (x)
(setq x (iter-yield (1+ x)))
(setq x (iter-yield (* 2 x))))
f
ELISP> (progn
(setq x (f 1))
…

shynur
- 4,065
- 1
- 3
- 23
2
votes
1 answer
Cycling through a list back and forth
I need an interactive function that works like a generator, whenever it is called it should return "next" or "previous" value from a list. Whenever it reaches the end of the list, it should start over from the beginning (and vice versa when going…

iLemming
- 1,223
- 9
- 14
2
votes
4 answers
Equivalent of `continue' in `cl-loop'?
Does the cl-loop macro implement an equivalent to the continue keyword of other languages?
The behavior of break can be achieved by using until or while clauses by placing them in the middle of cl-loop, e.g.
(cl-loop item in '(1 2 3 4)
do…

kdb
- 1,561
- 12
- 21
2
votes
1 answer
Infinite repetition (iteration) in Elisp code
There is an option to
repeat a macro an infinite number of times until the function quits (for example, by reaching the end of the buffer) or if the user cancels the command in keyboard macros
by using C-u0 as prefix to C-xe.
How can we mimic…

Sati
- 775
- 6
- 21
2
votes
1 answer
Iterate with both value and index
I want to iterate over a list with both the index and the value in the loop.
Currently, the code looks like this:
(let ((my-list '(a b c)))
(cl-loop for index below (length my-list)
for item = (seq-elt my-list index)
do…

Damien Cassou
- 877
- 4
- 14