Is there any possibility to modify a loop variable from within the loop?
E.g.
for i in 0..5:
if i == 2:
inc iAlthough this is probably not envisioned, it would be easier to read than:
var i = 0
while i <= 5
if i == 2:
inc i
inc iespecially it the loop contained conditional break s or continue s.
An alternative would be using an external variable for the loop's control like:
var i: int
for i in 0..5:
if i == 2:
inc iAlthough this is probably not envisioned, it would be easier to read than ...
No, it wouldn't. If you have unstructured iteration, it's nice to see that immediately thanks to the 'while'.
What about iterating over array and modifying it?
var arr : array[10, int] = ...
for v in arr:
v = 123
Would be a lot of use for arrays of aggregates. Is this somehow possible?
Would be a lot of use for arrays of aggregates. Is this somehow possible?
If you get the devel branch of Nim, it is indeed!:
var arr: array[10, int]
for v in arr.mitems:
v = 123There is mpairs as well for for i, v in arr.mpairs: ...