Skip to content

Commit

Permalink
Iteratorsize for partition iterator (#16552)
Browse files Browse the repository at this point in the history
  • Loading branch information
mschauer authored and rfourquet committed Jul 12, 2017
1 parent 8678b5e commit 0d83de9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions base/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -799,6 +799,11 @@ mutable struct PartitionIterator{T}
end

eltype(::Type{PartitionIterator{T}}) where {T} = Vector{eltype(T)}
partition_iteratorsize(::HasShape) = HasLength()
partition_iteratorsize(isz) = isz
function iteratorsize(::Type{PartitionIterator{T}}) where {T}
partition_iteratorsize(iteratorsize(T))
end

function length(itr::PartitionIterator)
l = length(itr.c)
Expand Down
7 changes: 7 additions & 0 deletions test/iterators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,13 @@ let v = collect(partition([1,2,3,4,5], 1))
@test all(i->v[i][1] == i, v)
end

let v1 = collect(partition([1,2,3,4,5], 2)),
v2 = collect(partition(flatten([[1,2],[3,4],5]), 2)) # collecting partition with SizeUnkown
@test v1[1] == v2[1] == [1,2]
@test v1[2] == v2[2] == [3,4]
@test v1[3] == v2[3] == [5]
end

let v = collect(partition([1,2,3,4,5], 2))
@test v[1] == [1,2]
@test v[2] == [3,4]
Expand Down

0 comments on commit 0d83de9

Please sign in to comment.