Is there an equivalent to "continue" (python) in rebol?
Asked Answered
S

3

5

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?

Sampler answered 7/9, 2013 at 13:36 Comment(0)
K
6

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.

Kindred answered 7/9, 2013 at 14:46 Comment(1)
The /name refinement was not so obvious, it worked after some RTFM, though: ` continue: does [throw/name 'continue 'keepgoing] repeat i 10 [ catch/name [ prin i if (i = 5) [continue] prin i ] keepgoing ] ` returns: 112233445667788991010 , as expected. – pierre 5 mins agoSampler
O
4

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.

Oratorical answered 7/9, 2013 at 14:11 Comment(0)
M
3

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.}
    ]
Mounting answered 8/9, 2013 at 8:43 Comment(1)
Yes, but this does not behave like I wish, and like python does: continue doesn't break out of the loop, it keeps going for next iterations of the loop.Sampler

© 2022 - 2024 — McMap. All rights reserved.