Is there an equivalent to C "break" and "continue" for 'do', 'do_idx' or 'to'


#1

For example:
{1 2 3 4}.do [if item = 2 [exit]] // ERROR: Not in the scope of a loop.
{1 2 3 4}.do_idx [if item = 2 [idx := 10000]] // works but ugly

How about “continue”?


#2

You can use either the any?() or all?() methods to do an early exit. Both are essentially the same but any?() stops on a true result and all?() stops on a false result.

{1 2 3 4}.any?[item = 2]

// You can have multiple expressions, just ensure to end with the test
{1 2 3 4}.any?[println(item) item = 2]

any?() will return true if there is an early exit otherwise it will return false - though you don’t have to use this result.

do(), do_idx(), any?(), all?() - are just simple routines in the List class rather than language primitives. You can easily build your own version if you would like a routine with slightly different characteristics. Take a look at the body of these routines to see how they are implemented. You can press Ctrl+G while on an identifier in a SkookumIDE editor window and it will pop you to its definition.


#3

Those look pretty good, thanks!