charje.loop
2026-11-29
Charles Ergonomic, Extensible, Efficient Loop Abstraction
Charles' Ergonomic, Extensible, Efficient Loop
Ergonomic: It has a consistent interface unlike cl:loop, iterate, series, and ANSI list operations (cl:mapcar, cl:remove-if-not, cl:reduce, cl:dolist). Two interfaces are provided that are compatible with each other; use whichever one makes you happy, or use them both together.
Extensible: You can define your own efters and gatherers that integrate tightly into other operations.
Efficient: All operations are non-consing when possible. It seems to be slightly slower than existing looping patterns, but the performance is comparable. Please contribute if you know how to make it faster.
Looping
Loop uses some symbols in the common-lisp package, so you can use package local nicknames or a :shadowing-import. These examples are in the charje.loop.user package.
Example of regular loop.
(loop (n (step :to 15)) (when (oddp n) (averaging n))) ;; 8
The same example without any local variables making use of binding-arrows.
(-> (step :to 15) (filter #'oddp) average) ;; 8
Looping over a list and a vector at the same time.
(loop ((base (list 1 2 3)) (power (vector 4 5 6))) (print (expt base power)))
The same example with the efting functions. Notice that the operations work with multiple values. Notice also that loop* is just like loop but for use in binding-arrows:->.
(-> (list 1 2 3) eft (zip (eft (vector 4 5 6))) (map #'expt) (loop* (n) (print n)))
see cookbook.md for more examples with comparisons to ANSI constructs.
Efters
eft /ɛft/
verb
- to do something many times; over and over again
- to do something to each thing in a set
An efter (/ɛftɝ/) is a function that takes no arguments and returns the next value of the sequence. Efter functions or data structures can be turned into efters that are the input to loop and loop*. For loop, the results of each efter get bound to a loop variable. You can leave off the extra set of parenthesis if there is only 1 efter. loop* takes an efter as its first argument; this is why it works with binding-arrows:->. If an efter returns multiple values, then put more than one binding variable.
(loop ((a <efter>) (b c <efter2>)) ;efter2 returns 2 values ...)
(loop (a <efter>) ;shorthand with less paraenthesis ...)
(-> <efter> (loop* (a b) ...))
An efter is a function that takes zero arguments and returns the next value (or values). When (if) there are no more values to eft it should call end-eft to stop efting.
An efter constructor is some means of creating a efter function. It may take anything as arguments including another efter. By these definitions, there is an efter built into ANSI CL: constantly which takes something as input and returns an after that returns that thing forever.
Efters should be non-consing. Efters should be inlined. Efters that deal with other Efters should account for them returning multiple values. Creating an efter should not do anything on its own other than setup. Stuff should start to happen when loop starts calling next (funcall) on the efter. Documentation for efters should talk about them as if they are not lazy.
Loop uses the eft generic-function to convert data to an efter. Feel free to define eft methods for your custom data structures.
See efters.lisp for examples.
Gatherers
Gatherers while not necessary are a convenient way to gather some data. Gatherers can be defined most conveniently using define-gathering-macro (see the docstring for more details). A function version of the gatherer for use in binding-arrows:-> can then be made trivially using the :arrow argument or loop.
Gatherers can tell loop to create local variables and symbol-macrolets.
see gatherers.lisp for examples.
Normal indentation for loop in Emacs
(progn (put 'loop 'sly-common-lisp-indent-function 1) (put 'loop* 'sly-common-lisp-indent-function 1))
see benchmark for some basic benchmarks.