apologies if this has been discussed before, but i was surprised it didn’t work when i naively tried it:
julia> for i in 1:10 if i>3
@info i
end
ERROR: syntax: incomplete: "for" at REPL[1]:1 requires end
Stacktrace:
[1] top-level scope at REPL[1]:0
strange because the if
clause works in comprehensions:
julia> [i for i in 1:10 if i>3]
7-element Array{Int64,1}:
4
5
6
7
8
9
10
has there been any discussion on making this possible? why wouldn’t we want them to work similarly?
One way you could do it is with a generator.
foreach( (i for i in 1:10 if i>3) ) do i
@info i
end
Or just (well, mostly) pressing return.
for i in 1:10
if i>3
@info i
end
end
2 Likes
You could probably put the logic inside the range like
for i in filter(x -> x > 3,1:10)
@info i
end
(I didn’t test this)
cshen
5
even more straight forward…
for i in 4:10
@info i
end
?
This syntax already means something:
julia> for i in 1:6 if i > 4
@info i
end end
[ Info: 5
[ Info: 6
In other words, you just need to add a second end
to close the if
block. Changing this would be a breaking change to where these blocks end.
17 Likes