Skip to content

Commit

Permalink
Define shape of truncated zips with HasShape{1} components
Browse files Browse the repository at this point in the history
(updated with the help of @martinholters.)
  • Loading branch information
mschauer committed May 7, 2019
1 parent b126788 commit e715cba
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
9 changes: 5 additions & 4 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
24 changes: 23 additions & 1 deletion test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,32 @@ 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
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)
@test length(z) == 6
end

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

0 comments on commit e715cba

Please sign in to comment.