This is a rough WIP draft to stimulate the discussion. ☺
Syntax:
(loop ?INTO BIND ITER ?KEY VAL)
where
INTO
is either {}
, []
, existing TABLE
, or (SYM ?VAL)
for accumulation
BIND
is either SYM
or (SYM SYM…)
to be bound by ITER
ITER
is either an iterator function or [MIN MAX ?STEP]
for numeric iteration
P.S. Too bad loop
cannot be named for
. ☹
Old: for
, each
, collect
, icollect
, fcollect
, accumulate
, faccumulate
New: loop
Design goals in no particular order:
No guard (or until
) yet. To pull its weight, loop
would have to elegantly
replace while
as well; otherwise, one can simply type if
or when
in the
loop.
Create a set of all integers in the given interval.
This is currently impossible with fcollect
because it produces a sequence.
(loop {} i [1 10] i true)
Before:
(icollect [_ elm (ipairs lst)] (* elm 2))
After:
(loop [] (_ elm) (ipairs lst) (* elm 2))
Before:
(accumulate [sum 0 _ elm (ipairs tbl)] (+ sum elm))
After:
(loop (sum 0) (_ elm) (ipairs tbl) (+ sum elm))
Note. (sum 0)
would read better as [sum 0]
, like in let
, but that would be ambiguous.
Before:
(each [idx (pairs tbl)] (proc idx))
After:
(loop idx (pairs tbl) (proc idx))