When writing nested loops, it would be good to be able to recur to an enclosing loop.
- Key condition: recur to an outer loop must be from tail position of inner loop, which must be in tail position of its enclosing loop, which must be ..., all the way up to the outer loop being recurred to
- Possible syntax:
(loop outer [...] (loop inner [...] ... (recur-to outer ...)))
Inspired by Scheme's named
:let
(let outer (...) (let inner (...) ... (outer ...)))
- Loop names optional
recur-to
only able to target named loops / fnsrecur
always targets innermost loop, whether named or not (enclosing fn when not in a loop)
- As well as the Scheme idiom, can be used to port code using named
for
loops withbreak
/continue
with label
- Current solutions:
letfn
with separate functions for each loop "layer", problematic with no TCE- returning a vector of new local values for outer loop at end of inner loop
- neither pretty nor performant
- particularly inconvenient when ultimately targeting a loop more than one layer away
ClojureScript branch with a proof of concept implementation (supports recur-to
named loops, but not named fns at this time):
https://github.com/michalmarczyk/clojurescript/tree/recur-to
Discussion on the dev list:
https://groups.google.com/d/msg/clojure-dev/imtbY1uqpIc/8DWLw8Ymf4IJ
Update: Clojure implementation:
Clojure branch with a complete implementation (supports recur-to
to named loops and named fns):
https://github.com/michalmarczyk/clojure/tree/recur-to
JIRA ticket:
https://dev.clojure.org/jira/browse/CLJ-2235
Discussion on the dev list:
https://groups.google.com/d/msg/clojure-dev/zlMGmv60MVA/beyIRTrhAgAJ