-
-
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 all 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,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 | ||
julia> a = 1:5 | ||
1:5 | ||
|
||
|
@@ -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)) | ||
|
@@ -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 | ||
""" | ||
|
||
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 |
||
|
||
```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} | ||
|
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,) | ||
@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` | ||
|
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.
This is still not quite right, try looking at enumerate on line 35.