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

WIP/ RFC/First PR: Add unzip to base.iterators.jl #21208

Closed
wants to merge 12 commits into from
35 changes: 32 additions & 3 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,9 @@ end
For a set of iterable objects, returns an iterable of tuples, where the `i`th tuple contains
the `i`th component of each input iterable.

Note that [`zip`](@ref) is its own inverse: `collect(zip(zip(a...)...)) == collect(a)`.
Note that the inverse of [`zip`](@ref) is [`unzip`](@ref).

```jldoctest
"""jldoctest
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still not quite right, try looking at enumerate on line 35.

julia> a = 1:5
1:5

Expand All @@ -222,7 +222,8 @@ julia> length(c)

julia> first(c)
(1, "e")
```
"""

"""
zip(a, b, c...) = Zip(a, zip(b, c...))
length(z::Zip) = _min_length(z.a, z.z, iteratorsize(z.a), iteratorsize(z.z))
Expand All @@ -240,6 +241,34 @@ end
iteratorsize{I1,I2}(::Type{Zip{I1,I2}}) = zip_iteratorsize(iteratorsize(I1),iteratorsize(I2))
iteratoreltype{I1,I2}(::Type{Zip{I1,I2}}) = and_iteratoreltype(iteratoreltype(I1),iteratoreltype(I2))

# unzip
"""

Copy link
Contributor

Choose a reason for hiding this comment

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

I would have thought that unzip would need it's own method name, something like unzip(iter::AbstractZipIterator). Also, I think that you need to add unzip to exports.jl.

For iterable objects of the same length, returns iterable sets, where the ith set contains
the ith component of each input iterable object.

Note that the inverse of [`unzip`](@ref) is [`zip`](@ref).
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps not necessary to reference unzip in its own documentation?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But zip's docs say that zip's inverse is unzip.
(Or at least tried to indicate the inverse.)
... Decision time?

Copy link
Member

Choose a reason for hiding this comment

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

I just meant the @ref part of unzip here, no need to link to itself. (And the docs for zip probably shouldn't have a @ref for itself either.)


```jldoctest
julia> z = [[1,"a",:meow], [2,"b",:woof], [3,"c",:doh!]]
3-element Array{Array{Any,1},1}:
Any[1, "a", :meow]
Any[2, "b", :woof]
Any[3, "c", :doh!]

julia> unzip(z)
Base.Iterators.Zip2{UnitRange{Int64},Array{Int64,1}}(1:3, [1, 2, 3])
Base.Iterators.Zip2{UnitRange{Int64},Array{String,1}}(1:3, String["a", "b", "c"])
Base.Iterators.Zip2{UnitRange{Int64},Array{Symbol,1}}(1:3, [:meow, :woof, :doh!])
```
"""
unzip(iter::AbstractZipIterator) = _unzip(iter)
# This avoids exporting the method that isn't for AbstractZipIterators
_unzip(iter::Zip1) = (iter.a,)
_unzip(iter::Zip2) = (iter.a, iter.b)
_unzip(iter::Zip) = (iter.a, _unzip(iter.z)...)
_unzip(iter) = (iter,)

# filter

struct Filter{F,I}
Expand Down
16 changes: 16 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ let z = zip(1:2, 3:4, 5:6)
@test eltype(z) == Tuple{Int,Int,Int}
end

# unzip
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this a good place to use @testset?

let z = zip(1:2)
@test unzip(z) == (1:2,)
@test eltype(unzip(z)) == UnitRange{Int}
end

let z = zip(1:2, 3:4)
@test collect(unzip(z)) == [(1,2), (3,4)]
@test eltype(unzip(z)) == UnitRange{Int}
end

let z = zip(1:2, 3:4, 5:6)
@test collect(unzip(z)) == [(1,2), (3,4), (5,6)]
@test eltype(unzip(z)) == UnitRange{Int}
end

@test eltype(Iterators.filter(isodd, 1:5)) == Int

# typed `collect`
Expand Down