Cycles
There is no special syntax for cycles in niva, everything is a message send.
For loops
Here ..
is a binary message for Int that creates IntRange
.
And IntRange
can receive forEach:
message.
with indexing:
The type signature of forEachIndexed
here isIntRange forEachIndexed: [Int, Int -> Any]
Which means that it takes a codeblock that receive 2 parameters, one of them is index and second is the number itself.
With custom steps:
While loop
While loop is just a message for the codeblock: [ -> Boolean] whileTrue: [ -> Unit]
Iterate over collections
Over map:
See collections chapter for more
Ranges
Here are some useful messages for ranges
Iterate with break
If you really need to break while iterating collection you can add this method to your codebase:
And use it like:
So it will print 1 2 3 and break 3 is divisible by 3 without a remainder.
Basically it can be replaced with whileTrue:
msg