1

I'm a huge fan of using generators all the time and for everything, but sometimes you just need to convert an iterator to a list.

The documentation on generators seems to imply this is doable with the loop facility

The Common Lisp loop facility also contains features for working with iterators. See Loop Facility in Common Lisp Extensions.

Yet I cannot find anything about generators or iterators in the loop facility docs.

I assumed that something like this would work

  (iter-defun gim/playground/a-generator-function ()
    (iter-yield 5)
    (iter-yield 9)
    (iter-yield -28))
  
  (cl-loop for x in (gim/playground/a-generator-function) collect x)

But it doesn't seem to iterate the generator result, just the list that represents the iterator. So how do I

  1. Elegantly convert an iterator to a list (yes, assuming it is finite and possible)
  2. Use the loop facility with iterators
George Mauer
  • 397
  • 2
  • 9
  • 1
    Use `iter-by`. `(cl-loop for x iter-by (GENERATOR) collect r)`. – Tobias May 26 '22 at 16:30
  • Goodness, yes now I see it at the bottom of the "iteration clauses" page, just didn't see it and it was weird that the generator doc pointed to a page that didn't have the information – George Mauer May 27 '22 at 02:07

0 Answers0