Nested Ranges in Generator

What is the generator syntax where the value of variables from one range can be used as limits for another range?

collect([a,b] for a=1:2, b=a:a+1)
ERROR: UndefVarError: a not defined

Expecting

 [1, 1]  [1, 2]
 [2, 2]  [2, 3]

Here’s an example where b does not depend on a.

collect([a,b] for a=1:2, b=4:5)
2×2 Array{Array{Int64,1},2}:
 [1, 4]  [1, 5]
 [2, 4]  [2, 5]

welcome to discourse :slight_smile:

see https://docs.julialang.org/en/v1/manual/arrays/#Generator-Expressions-1

Ranges in generators and comprehensions can depend on previous ranges by writing multiple for keywords

julia> collect([a,b] for a=1:2 for b=a:a+1)
4-element Array{Array{Int64,1},1}:
 [1, 1]
 [1, 2]
 [2, 2]
 [2, 3]
3 Likes