Hello world. I was trying this morning, to loop on two seqs at the same time, but in a reverse order. For example:
var
seq1 = @[ #[First seq's content]# ]
seq2 = @[ #[Second seq's content]# ]
for index in countdown(seq1.len, 0):
echo (seq1[index] + seq2[index])
But I faced an issue: The two sequences didn't have the same length. I finaly found another way to implement my algorithm. But i'm still asking myself: Isn't there any manner to do a double loop ? What about adding that to Nim ? I imagine something like this:
var
seq1 = @[ #[First seq's content]# ]
seq2 = @[ #[Second seq's content]# ]
for
index1 in countdown(seq1.len, 0)
index2 in countdown(seq2.len, 0)
: # When the length of the shortest seq is reached, the loop stops
echo (seq1[index1] + seq2[index2])
Sometimes a tiny amount of math does the trick:
var
seq1 = @[ #[First seq's content]# ]
seq2 = @[ #[Second seq's content]# ]
for index in countdown(min(seq1.len, seq2.len)-1, 0):
echo(seq1[index] + seq2[index])
or maybe you want to loop over the tail of the sequences. here is this option compared with @araq's way in a simple example (playground):
var
seq1 = @[ 1, 2, 3 ]
seq2 = @[ 1, 2, 3, 4, 5, 6 ]
for index in countdown(min(seq1.len, seq2.len)-1, 0):
echo((seq1[index], seq2[index], seq1[index] + seq2[index]))
echo ""
for index in 1 .. min(seq1.len, seq2.len):
echo((seq1[^index], seq2[^index], seq1[^index] + seq2[^index]))
output:
(3, 3, 6)
(2, 2, 4)
(1, 1, 2)
(3, 6, 9)
(2, 5, 7)
(1, 4, 5)