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
28 changes: 27 additions & 1 deletion base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ 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
julia> a = 1:5
Expand Down Expand Up @@ -240,6 +240,32 @@ 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
```jldoctest
Copy link
Member

Choose a reason for hiding this comment

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

Only the code should be within ```jldoctest ... ``` (i.e. the parts with julia>)

Also, the complete docstring should be within """ ... """, take a look at the docstring for zip on the lines above in this file!

(Also note that the examples are outdated after you updated unzip to only work with AbstractZipIterators)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

OK, tried to fix this up.
Review comments?


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.)


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)
Array{Int64,1}:[1,2,3]
Array{String,1}["a","b","c"]
Array{Symbol,1}[: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)...)
Copy link
Member

Choose a reason for hiding this comment

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

The inner call to unzip here should actually be _unzip. (That was my typo initially, sorry about that.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha. Corrected now.

_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
Copy link
Member

Choose a reason for hiding this comment

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

Since unzip always returns a Tuple, the right hand side of this test should be (1:2,)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Gotcha. Corrected as well.

@test eltype(unzip(z)) == UnitRange{Int64}
Copy link
Member

Choose a reason for hiding this comment

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

UnitRange{Int} here since it will fail on systems with Int == Int32 (and same on the tests below)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! Corrected now.

end

let z = zip(1:2, 3:4)
@test collect(unzip(z)) == [(1,2), (3,4)]
@test eltype(unzip(z)) == UnitRange{Int64}
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{Int64}
end

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

# typed `collect`
Expand Down