-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 9 commits
0360089
a1ebd47
2df8ecd
9505283
13ac9df
e7fab27
6b3a9de
5508090
e4c7515
75537c1
c9ea231
2b98ccc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would have thought that |
||
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). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps not necessary to reference There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just meant the |
||
|
||
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)...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The inner call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. Corrected now. |
||
_unzip(iter) = (iter,) | ||
|
||
# filter | ||
|
||
struct Filter{F,I} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,22 @@ let z = zip(1:2, 3:4, 5:6) | |
@test eltype(z) == Tuple{Int,Int,Int} | ||
end | ||
|
||
# unzip | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't this a good place to use |
||
let z = zip(1:2) | ||
@test unzip(z) == 1:2 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. Corrected as well. |
||
@test eltype(unzip(z)) == UnitRange{Int64} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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` | ||
|
There was a problem hiding this comment.
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 withAbstractZipIterators
)There was a problem hiding this comment.
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?