Skip to end of metadata
Go to start of metadata

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 / fns
      • recur 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 with break / 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):

Discussion on the dev list:

Update: Clojure implementation:

Clojure branch with a complete implementation (supports recur-to to named loops and named fns):

JIRA ticket:

Discussion on the dev list: