I am converting some python code to rebol, and I just came across a "continue" instruction. It interrupts the processing of a loop, going to the next iteration of the loop.
I find this word quite elegant and readable.
I came across some answers there: (in French), but nothing really "elegant":
http://pl.legoff.free.fr/dotclear/vf/index.php/post/2003/01/05/Equivalent-d-un-Continue-ou-Next-dans-une-bouc
As this converation is about 10 years old, maybe some improvements were made in Rebol since?
You can do it in Rebol2 using exceptions:
continue: does [throw 'continue]
loop 2 [
catch [
print {"This'll print", {DocKimbel} said.}
continue
print {"This won't print", {DocKimbel} said.}
]
]
In case you have custom exceptions to handle in the loop, you can use the /name refinement as in catch/name [...] 'continue to avoid catching other exceptions. It is even possible to override iterators to do it transparently for you, but at the cost of some slower performances.
CONTINUE is not in Rebol2. But it's in Rebol3 and works just fine:
loop 2 [
print {"This'll print", {HostileFork} said.}
continue
print {"This won't print", {HostileFork} said.}
]
You'll get the output:
"This'll print", {HostileFork} said.
"This'll print", {HostileFork} said.
There is no way, as far as I know, to implement a continue in Rebol2.
Okay, building on @DocKimbel's answer a bit, you could do this in Rebol2:
old-loop: :loop
loop: func [count [integer!] block [block!]] [
old-loop count [catch block]
]
continue: does [throw 'continue]
loop 2 [
print {"This'll print", {HostileFork} said.}
continue
print {"This won't print", {HostileFork} said.}
]
Note: the internal implementation method for BREAK and CONTINUE does use the same mechanism as THROW in Rebol...which is relatively lightweight and not exception handling. So important to know is that THROW is not what you should be using for errors...and modern Rebol builds (Ren/C) will not even allow you to throw one. You should use FAIL instead.
In R2 LOOP 1 works too, with less overhead than CATCH
loop 1 [
print {"This'll print", {DocKimbel} said.}
break
print {"This won't print", {DocKimbel} said.}
]