Nested for
loops may be used to iterate over a number of unique iterables.
result = []
for a = iterable_a
for b = iterable_b
push!(result, expression)
end
end
Similarly, multiple iteration specifications may be supplied to an array comprehension.
[expression for a = iterable_a, b = iterable_b]
For example, the following may be used to generate the Cartesian product of 1:3
and 1:2
.
julia> [(x, y) for x = 1:3, y = 1:2]
3×2 Array{Tuple{Int64,Int64},2}:
(1,1) (1,2)
(2,1) (2,2)
(3,1) (3,2)
Flattened multidimensional array comprehensions are similar, except that they lose the shape. For example,
julia> [(x, y) for x = 1:3 for y = 1:2]
6-element Array{Tuple{Int64,Int64},1}:
(1, 1)
(1, 2)
(2, 1)
(2, 2)
(3, 1)
(3, 2)
is a flattened variant of the above. The syntactic difference is that an additional for
is used instead of a comma.