The purpose of the for i, j
form is to create a shaped array (2D in this case) and the i, j
syntax is consistent with the array indexing syntax: first rows then columns. So the number of elements for j
cannot depend on i
. This syntax is inherently more restrictive than for ... for ...
.
The order of iterations in for i, j
is consistent with the “right order” of iteration for most arrays: since Julia arrays are column-major by default, it’s generally best to iterate first on the row index (i.e. along a column). In particular, the Array
created by array comprehension is indeed column-major, so using this order means Julia can fill the new array in the most efficient way. But as @mcabbott says it’s probably best to use this syntax only when the order doesn’t matter anyway.
For me the questionable choice is to allow for i, j
outside of comprehensions/generators. It’s confusing indeed that the order is not the same as in a comprehension, and I wish the syntax was simply not allowed. But once you decide to allow it, it makes sense to use the lexical order of iteration because in this case i
and j
have no defined association with rows and columns, and the reverse-lexical order would be even more confusing…