So here’s an annoying bit, if applies a filter to the elements, and it removes the multiple dimensions from the multidimensional syntax:
julia> [(r, c) for r in 1:2, c in 1:3]
2×3 Matrix{Tuple{Int64, Int64}}:
(1, 1) (1, 2) (1, 3)
(2, 1) (2, 2) (2, 3)
julia> [(r, c) for r in 1:2, c in 1:3 if iseven(c+r)]
3-element Vector{Tuple{Int64, Int64}}:
(1, 1)
(2, 2)
(1, 3)
We just get a vector of the allowed elements. However, this is not falling back to the nested for behavior, which would reverse the axes’ order:
julia> [(r, c) for r in 1:2 for c in 1:3 if iseven(c+r)]
3-element Vector{Tuple{Int64, Int64}}:
(1, 1)
(1, 3)
(2, 2)
I only use the latter because looking more like the nested for-loop helps, and it never made sense to me to filter a multidimensional syntax and sacrifice the multidimensionality.
We are also unable to filter an axis on its own:
julia> [(r, c) for r in 1:2 if r > 1, c in 1:3]
ERROR: ParseError:
# Error @ REPL[57]:1:30
[(r, c) for r in 1:2 if r > 1, c in 1:3]
# └ ── Expected `]`
A filter depending on 2 axes like for i=1:4; j=1:3 if i>j for k=5:6 would go a step further than that.
In my experience, if works in the nested for syntax as expected from the corresponding loop, but you should check your examples in the REPL.
If we’re going that far (in a breaking change), I’d rather not write for at all. for just makes me think for-loop, and I reasonably expect that’s true for most people. Multidimensional array comprehension is an outlier, and taking a cue from 1-dimensional set/list comprehension syntax was going to be weird.
Interesting that you phrase it as iteration because I can’t find a clear specification. The comprehension docs don’t really say how it’s iterated, but the implementation of a generator iterates a composite Iterators.product that changes the leftmost iterators first (column-major order iteration does not imply storage), and both linear indexing and the optional IndexLinear trait specify column-major order iteration for AbstractArray. eachindex seems to leave iteration order open, but I don’t know of an array type that diverges from column-major order, nor do I know what happens if we mix it with typical objects e.g. would RowMajorIterationArray{Tuple{Int,Int}}(undef, 2, 3) .= ((r, c) for r in 1:2, c in 1:3) result in an array equal to a Array comprehension?
