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

Define shape of some truncated zips #29927

Merged
merged 9 commits into from Apr 5, 2020
Merged

Conversation

mschauer
Copy link
Contributor

@mschauer mschauer commented Nov 4, 2018

"Closes" #17928 (#17928 is closed, but should be open and would be closed... )

Zip truncates iterators to the shortest length. Currently, length reports the effective length if possible, but size gives a DimensionMismatch. For a zip iterator z with one-dimensional components (HasShape{1} components) defining the size as (length(z), ) and axes correspondingly allows fast collection and it is also what collect expects to be defined as the iterator itself has HasShape{1}. But it is not clear how to define the size of truncated higher-dimensional iterators, so this is spared out. This makes the PR rather small and it should be not-breaking.

#20499 was closed so I think truncating zips are a reality now, I added a remark to the doc string.

@KristofferC KristofferC added the needs news A NEWS entry is required for this change label Nov 4, 2018
@martinholters
Copy link
Member

Needs to updated because of #29238, but I think the general idea is right.

@mschauer
Copy link
Contributor Author

mschauer commented May 7, 2019

@martinholters Are you willing to look into this? It is perhaps easier for you than for me to understand what has to be done to achieve the same with the new parametrization.

@martinholters
Copy link
Member

I think this a relatively direct translation of your approach:

diff --git a/base/iterators.jl b/base/iterators.jl
index bebe8ac170..0d52ca4c2a 100644
--- a/base/iterators.jl
+++ b/base/iterators.jl
@@ -312,10 +312,11 @@ function _zip_min_length(is)
     end
 end
 _zip_min_length(is::Tuple{}) = nothing
-size(z::Zip) = _promote_shape(map(size, z.is)...)
-axes(z::Zip) = _promote_shape(map(axes, z.is)...)
-_promote_shape(a, b...) = promote_shape(a, _promote_shape(b...))
-_promote_shape(a) = a
+size(z::Zip) = mapreduce(size, _zip_promote_shape, z.is)
+axes(z::Zip) = mapreduce(axes, _zip_promote_shape, z.is)
+_zip_promote_shape((a,)::Tuple{OneTo}, (b,)::Tuple{OneTo}) = (intersect(a, b),)
+_zip_promote_shape((m,)::Tuple{Integer},(n,)::Tuple{Integer}) = (min(m,n),)
+_zip_promote_shape(a, b) = promote_shape(a, b)
 eltype(::Type{Zip{Is}}) where {Is<:Tuple} = _zip_eltype(Is)
 _zip_eltype(::Type{Is}) where {Is<:Tuple} =
     tuple_type_cons(eltype(tuple_type_head(Is)), _zip_eltype(tuple_type_tail(Is)))

Please feel free to go ahead and update your PR with this.

@mschauer
Copy link
Contributor Author

mschauer commented May 7, 2019

Thank you! I updated the PR with your help.

@mschauer
Copy link
Contributor Author

mschauer commented May 7, 2019

Maybe helpful as a summary:

Before

julia>  [i for i in zip(1:3, 2:5)]
ERROR: DimensionMismatch("dimensions must match")

julia> for i in  zip(1:3, 2:5); println(i);end
(1, 2)
(2, 3)
(3, 4)

Now

julia>  [i for i in zip(1:3, 2:5)]
3-element Array{Tuple{Int64,Int64},1}:
 (1, 2)
 (2, 3)
 (3, 4)

julia> for i in  zip(1:3, 2:5); println(i);end
(1, 2)
(2, 3)
(3, 4)

@mschauer
Copy link
Contributor Author

mschauer commented May 8, 2019

Regarding the news, would this be considered a change, a fix, or an extension of functionality? The technical description of the change would be:

  • The size of a Zip of 1-d iterators (i.e. those where Base.IteratorSize is
    Base.HasShape{1}()) is now (length(n),) where n is the length of the zipped iterator.

@mschauer
Copy link
Contributor Author

Ping?

@StefanKarpinski StefanKarpinski added the triage This should be discussed on a triage call label May 22, 2019
@mschauer
Copy link
Contributor Author

I added a news item.

@mschauer
Copy link
Contributor Author

When would be a good time for you to review this?

@AzamatB
Copy link
Contributor

AzamatB commented Sep 5, 2019

Bump. I was just bitten by this.

@mschauer
Copy link
Contributor Author

mschauer commented Oct 3, 2019

Ping

@mschauer
Copy link
Contributor Author

mschauer commented Oct 4, 2019

Closes #33340, #33168

@mschauer
Copy link
Contributor Author

@JeffBezanson Please allow me to ping you on this.

@StefanKarpinski
Copy link
Sponsor Member

@maleadt, how do we run PkgEval on this?

@maleadt
Copy link
Member

maleadt commented Jan 3, 2020

@nanosoldier runtests(ALL, vs = ":master")

I had to look it up as well... (It's only part of the README in JuliaCI/Nanosoldier.jl#64)

@nanosoldier
Copy link
Collaborator

Your test job has completed - possible new issues were detected. A full report can be found here. cc @maleadt

@mschauer
Copy link
Contributor Author

mschauer commented Jan 8, 2020

Looks good. Nanosoldier reports 7 packages failed tests only on the current version, 10 packages passed tests only on the current version. Failures are due to lack of random seeding or time outs.

@mschauer
Copy link
Contributor Author

The release candidate for Julia v1.4.0 is out, does this mean this also won't make the cut for 1.4 anymore??

@StefanKarpinski
Copy link
Sponsor Member

@mschauer
Copy link
Contributor Author

I see, so maybe 1.5.

@AzamatB
Copy link
Contributor

AzamatB commented Jan 30, 2020

Bump

1 similar comment
@mschauer
Copy link
Contributor Author

mschauer commented Apr 3, 2020

Bump

@StefanKarpinski
Copy link
Sponsor Member

If CI passes I'm going to merge. This hasn't gotten discussed, but my general approach with stalled PRs that can't get dev attention is to merge so as to prevent passive stonewalling.

@rfourquet rfourquet removed the needs news A NEWS entry is required for this change label Apr 4, 2020
NEWS.md Outdated Show resolved Hide resolved
(2, 5)
(3, 6) ]
@test eltype(z) == Tuple{Int,Int}
@test_throws DimensionMismatch size(z)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a test for axes ?

(-2, -2) (2, 2)
(-3, -3) (3, 3)]
@test eltype(z) == Tuple{Int,Int}
@test size(z) == (3, 2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a test for axes ?

Co-Authored-By: Rafael Fourquet <fourquet.rafael@gmail.com>
@mschauer
Copy link
Contributor Author

mschauer commented Apr 5, 2020

I incorporated @rfourquet ’s suggestions

@rfourquet rfourquet merged commit c973ad8 into JuliaLang:master Apr 5, 2020
@rfourquet
Copy link
Member

Thank you @mschauer and sorry this took so long to get merged!

@rfourquet rfourquet added collections Data structures holding multiple items, e.g. sets and removed triage This should be discussed on a triage call labels Apr 5, 2020
ravibitsgoa pushed a commit to ravibitsgoa/julia that referenced this pull request Apr 9, 2020
@mschauer mschauer deleted the zipshape branch April 10, 2020 07:57
ztultrebor pushed a commit to ztultrebor/julia that referenced this pull request Apr 17, 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

Successfully merging this pull request may close these issues.

None yet

8 participants