Skip to content

adding tests to unzip #21212

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

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ 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

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

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!]
```
function unzip(z)
[z[j][i] for i=1:length(z), j=length(z)]
end


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