Is there a concise way to express "[println(i, j) for i = 1 : j, j = 1:3]"

Hi, is there a concise way to express the “for” circulation like “[println(i, j) for i = 1 : j, j = 1:3]”, I encounter the “ERROR: UndefVarError: j not defined”. I thought such an expression is more concise than

for j = 1:3, i = 1:j
println(i, j)
end

Maybe

julia> [println(i, j) for j in 1:3 for i in 1:j];
11
12
22
13
23
33

Note the ; to drop the wastefully generated vector.

Or:

julia> foreach(splat(println),((i,j) for j in 1:3 for i in 1:j))
11
12
22
13
23
33

with less waste.

3 Likes

thank you, Dan! I just found the comma between “j in 1:3” and “i in 1:j” in my code should be replaced with “for”!

1 Like

I just tied “[println(i, j) for j in 1:3, i in 1:3]” and it worked, astonishing!

1 Like

If you really don’t need the output vector, for j = 1:3, i = 1:j println(i, j) end in one line works and is shorter than that array comprehension, newlines aren’t needed for most blocks (I almost said all but off the top of my head, let’s header is an exception) and can be replaced with ; if needed.

3 Likes