DEV Community

Discussion on: Which types of loops are most popular in the programming languages you use?

Collapse
 
temporal profile image
Jacek Złydach
(loop for i in *random*
   counting (evenp i) into evens
   counting (oddp i) into odds
   summing i into total
   maximizing i into max
   minimizing i into min
   finally (return (list min max total evens odds)))

Or, more realistic example from a project I'm currently working on:

(loop
   with entities = (%get-entities-for-rendering x (+ x w) y (+ y h))
   for (ent-x ent-y ent-fg ent-char) in entities
   for realx = (+ cx (- ent-x x))
   for realy = (+ cy (- ent-y y))
   do
     (tcod:console-set-char-foreground console realx realy ent-fg)
     (tcod:console-set-char console
                            realx
                            realy
                            ent-char))

Common Lisp's loop can do crazy amount of things in a compact form, though there are things that are missing, and the whole thing isn't extensible (though there are library-provided alternatives like iterate that are better in this aspect).