Hi,
One thing which I have found inconvenient about loop/recur on multiple
occasions is that it only allows the innermost loop as the target (and
the enclosing fn only if not in a loop form). It's come up on the main
mailing list just now:
https://groups.google.com/d/msg/clojure/-oPnklPSLx8/j4Euim4XCycJ
prompting me finally to implement the idea I had about this. So, I'm
wondering if something like this would be considered desirable in
Clojure?
The syntax I have in mind is (loop loop-name [...] ...) / (recur-to
loop-name ...), where loop-name is optional in the loop and plain
recur can always be used to recur to the innermost loop, regardless of
whether it is named or not.
Here's an example in ClojureScript:
(let [arr (make-array 10)]
(dotimes [i 10]
(aset arr i (make-array 10)))
(loop outer [i 0]
(if (< i 10)
(loop inner [j 0]
(if (< j 10)
(do (aset arr i j :foo)
(recur (inc j)))
(recur-to outer (inc i)))) arr)))
;; returns a 10x10 2D array filled with :foos
I have implemented a proof of concept in ClojureScript. For now, it
only handles recur-to for named loops; presumably recur-to with named
fns should be possible as well. It's available here if you'd like to
give it a try:
https://github.com/michalmarczyk/clojurescript/tree/recur-to
The actual commit:
https://github.com/michalmarczyk/clojurescript/commit/feba0a078138da08d584a67e671415fc403fa093
The implementation takes advantage of JavaScript's support for
labelling loops. (Of course Java allows that too.)
Cheers,
Michał