Any shortcut to 1:length(myVector)?

Just go with:

for value in container – when you only care about iterating the values.
for index in eachindex(container) – when you gonna just use the indexes, or the values are used very infrequently. EDIT: see below [1].
for (index, value) in pairs(container) – when you gonna need both at each loop; or if you are using some more complex structure like Dict/trees and this may save some effort of hashing keys (or searching the tree) again.

[1] To complement: For simple code over Vectors there will probably no performance difference between eachindex + container[index] and pairs. If you gonna apply SSOT (Single Source of Truth) and you want to query the container every time instead of having bindings with cached values, then go for it. It may be better than using pairs in cases that, inside a single loop, the values may change inside the container and you always want to get the most recent values. But, theoretically, the performance of pairs will always be the same or better than eachindex + container[index] if both index and value are used.

15 Likes