Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comprehension ranging over empty sequence gives error if that variable is not the last one #19347

Closed
oantolin opened this issue Nov 17, 2016 · 2 comments

Comments

@oantolin
Copy link

oantolin commented Nov 17, 2016

I'm using Julia Version 0.5.0 (2016-09-19 18:14 UTC) and noticed that in a comprehension you can't iterate over empty sequences unless that variable is the last one. (EDIT: I just tested in the binaries of the nightly builds and the same thing happens.)

I'd expect both of these comprehensions to result in empty arrays:

julia> [a for a in [1] for b in []]
0-element Array{Int64,1}

julia> [a for a in [] for b in [1]]
ERROR: ArgumentError: argument to Flatten must contain at least one iterator
 in start(::Base.Flatten{Base.Generator{Array{Any,1},##193#195}}) at ./iterator.jl:599
 in grow_to!(::Array{Any,1}, ::Base.Flatten{Base.Generator{Array{Any,1},##193#195}}) at ./array.jl:357
 in collect(::Base.Flatten{Base.Generator{Array{Any,1},##193#195}}) at ./array.jl:273

Or a list of triples (a,b,a+b) where 1<=a<b<5 and a+b is prime:

julia> [(a,b,c) for a=1:5 for b=a+1:5 for c in [a+b] if isprime(c)]
ERROR: ArgumentError: argument to Flatten must contain at least one iterator
 in start(::Base.Flatten{Base.Generator{UnitRange{Int64},##18#21{Int64}}}) at ./iterator.jl:599
 in next at ./iterator.jl:614 [inlined]
 in grow_to!(::Array{Tuple{Int64,Int64,Int64},1}, ::Base.Flatten{Base.Generator{UnitRange{Int64},##17#20}}, ::Tuple{Int64,Base.Flatten{Base.Generator{UnitRange{Int64},##18#21{Int64}}},Tuple{Int64,Base.Generator{Filter{#isprime,Array{Int64,1}},##19#22{Int64,Int64}},Tuple{Bool,Int64,Int64}}}) at ./array.jl:364
 in grow_to!(::Array{Union{},1}, ::Base.Flatten{Base.Generator{UnitRange{Int64},##17#20}}, ::Tuple{Int64,Base.Flatten{Base.Generator{UnitRange{Int64},##18#21{Int64}}},Tuple{Int64,Base.Generator{Filter{#isprime,Array{Int64,1}},##19#22{Int64,Int64}},Tuple{Bool,Int64,Int64}}}) at ./array.jl:372
 in grow_to!(::Array{Tuple{Int64,Int64,Int64},1}, ::Base.Flatten{Base.Generator{UnitRange{Int64},##17#20}}) at ./array.jl:357
 in collect(::Base.Flatten{Base.Generator{UnitRange{Int64},##17#20}}) at ./array.jl:273

Oops, that last value of a let to an empty range for b, better skip it:

julia> [(a,b,c) for a=1:4 for b=a+1:5 for c in [a+b] if isprime(c)]
5-element Array{Tuple{Int64,Int64,Int64},1}:
 (1,2,3)
 (1,4,5)
 (2,3,5)
 (2,5,7)
 (3,4,7)
@oantolin
Copy link
Author

Sorry, it seems this is just a duplicate of #18852, where it is explained that this behavior is currently intentional as a hacky way of getting type stability.

@KristofferC
Copy link
Member

Closing as dup of #18852

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants