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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ New library features

Standard library changes
------------------------
* A 1-d `Zip` iterator (where `Base.IteratorSize` is `Base.HasShape{1}()`) with defined length of `n` has now also size of `(n,)` (instead of throwing an error) ([#29927]).
mschauer marked this conversation as resolved.
Show resolved Hide resolved
* The `@timed` macro now returns a `NamedTuple` ([#34149])

#### LinearAlgebra
Expand Down Expand Up @@ -64,4 +65,5 @@ Tooling Improvements
---------------------



<!--- generated by NEWS-update.jl: -->
9 changes: 5 additions & 4 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,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)))
Expand Down
33 changes: 32 additions & 1 deletion test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,41 @@ let z = zip(1:2)
@test eltype(z) == Tuple{Int}
end

let z = zip(1:2, 3:4)
for z in (zip(1:2, 3:4), zip(1:2, 3:5))
@test collect(z) == [(1,3), (2,4)]
@test eltype(z) == Tuple{Int,Int}
@test size(z) == (2,)
@test axes(z) == (Base.OneTo(2),)
@test length(z) == 2
end

let z = zip(1:2, Iterators.countfrom(3))
@test collect(z) == [(1,3), (2,4)]
@test eltype(z) == Tuple{Int,Int}
@test_throws MethodError size(z) # by convention, the zip of a finite and
# an infinite iterator has only `length`
@test length(z) == 2
mschauer marked this conversation as resolved.
Show resolved Hide resolved
end

let z = zip([i*j for i in 1:3, j in -1:2:1], 1:6)
@test collect(z) == [(-1, 1)
(-2, 2)
(-3, 3)
(1, 4)
(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 ?

mschauer marked this conversation as resolved.
Show resolved Hide resolved
@test length(z) == 6
end

let z = zip([i*j for i in 1:3, j in -1:2:1], [i*j for i in 1:3, j in -1:2:1])
@test collect(z) == [(-1, -1) (1, 1)
(-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 ?

mschauer marked this conversation as resolved.
Show resolved Hide resolved
@test length(z) == 6
end

let z = zip(1:2, 3:4, 5:6)
Expand Down