There are several iteration mechanisms in Lumen. The simplest is a while
loop:
(let i 3
(while (> i 0)
(print (dec i))))
The shorthand for iterating from 0 to N is for
:
(for i 3
(print i))
You can enumerate the keys and values of a list with each
:
(each (k v) (list 1 2 a: 10 b: 20)
(print (cat k " " v)))
(each v (list 1 2 a: 10 b: 20) ; values only
(print v))
(each (k (a b)) ; destructuring
(list (list 10 20) bar: (list "a" "b"))
(print (cat k " " a " " b)))
each
will bind keys and values in any order. If you want only the positional values of a list, you can enumerate them in order using step
:
(step x (list 1 2 3)
(print x))
(step (a b) (list (list 1 2) (list 10 20)) ; destructuring
(print a)
(print b))