Thx you all. I' was trying to find a way to make the same of this C# code with
a similar syntax:
for (int x = 0, y = 0; x <= 10 && y <= 10; x++, y++)
{
Console.WriteLine(x + y);
}
but i got lost on the way
The a, b are not actually start/end values. They are arbitrary elements from
the first and second slices (r[0] and r[1]).
Maybe Slice should use (slice_start, slice_end) for naming instead of (a, b).
But this is Nim, you can do Anything (TM) :D
iterator pairs(r: array[0 .. 1, HSlice[system.int, system.int]]): tuple[a:
int, b: int] =
for r1 in r[0]:
for r2 in r[1]:
yield (r1, r2)
for x, y in [0 .. 3, 0 .. 3]:
echo x, " ", y
Run
This will help you understand that code better:
import strformat, typetraits
let r = [0 .. 3, 0 .. 3]
echo fmt"r = {r}, of type `{$r.type}'"
for x, y in r:
echo fmt"x = {x}, of type `{$x.type}'"
echo fmt"y = {y}, of type `{$y.type}'"
Run
Ou
the for loop is iterating over the array and not the ranges in the array. afaik
you can't iterate over two ranges like that in nim.
Hi there. I'm doing some experiments with Nim. Can anyone explain how this code:
for x, y in [0..10, 0..10]:
echo(x)
echo(y)
Run
gives this output:
0 (a: 0, b: 10) 1 (a: 0, b: 10)
and is there a way to work this 2 or more items in a for loop? Thx