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

zip iterators of different length #36308

Closed
cossio opened this issue Jun 16, 2020 · 3 comments
Closed

zip iterators of different length #36308

cossio opened this issue Jun 16, 2020 · 3 comments
Labels
collections Data structures holding multiple items, e.g. sets

Comments

@cossio
Copy link
Contributor

cossio commented Jun 16, 2020

It seems inconsistent that collect(zip(1:3, 1:10)) throws a DimensionMismatch error:

julia> collect(zip(1:3, 1:10))
ERROR: DimensionMismatch("dimensions must match: a has dims (Base.OneTo(3),), b has dims (Base.OneTo(10),), mismatch at 1")
Stacktrace:
 [1] promote_shape at ./indices.jl:178 [inlined]
 [2] _promote_shape at ./iterators.jl:318 [inlined]
 [3] axes at ./iterators.jl:317 [inlined]
 [4] _similar_for at ./array.jl:578 [inlined]
 [5] _collect(::UnitRange{Int64}, ::Base.Iterators.Zip{Tuple{UnitRange{Int64},UnitRange{Int64}}}, ::Base.HasEltype, ::Base.HasShape{1}) at ./array.jl:609
 [6] collect(::Base.Iterators.Zip{Tuple{UnitRange{Int64},UnitRange{Int64}}}) at ./array.jl:603
 [7] top-level scope at REPL[2]:1

but

for (i,j) in zip(1:3, 1:10)
...
end

runs fine. Also the docs for zip say that it will just stop when the first iterator runs out, not that it will error if the iterators have different lengths.

In general I think we should allow zip to accept iterators of different length and be consistent with this.

@cossio
Copy link
Contributor Author

cossio commented Jun 16, 2020

Another weird example:

g = (i for i = 1:10^6 if iseven(i))
zip(1:10, 1:3, g) |> collect # works without error
zip(1:10, 1:3) |> collect # DimensionMismatch error

@jakobnissen
Copy link
Contributor

This is indeed a bug. It looks like axes is wrongly implemented for zip.

IteratorSize returns HasShape{1}(), but the documentation for IteratorSize states that anything has returns a HasShape should have axes defined for it. axes(::Zip) uses promote_shape, which does not reflect the actual size of a zip object.

@rfourquet
Copy link
Member

This was likely fixed by #29927 (I can't reproduce the reported errors on Julia 1.5).

@rfourquet rfourquet added the collections Data structures holding multiple items, e.g. sets label Jun 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
collections Data structures holding multiple items, e.g. sets
Projects
None yet
Development

No branches or pull requests

4 participants