From 0d83de93eb208e71aa5701329551939715268658 Mon Sep 17 00:00:00 2001 From: Moritz Schauer Date: Wed, 12 Jul 2017 10:37:07 +0200 Subject: [PATCH] Iteratorsize for partition iterator (#16552) --- base/iterators.jl | 5 +++++ test/iterators.jl | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/base/iterators.jl b/base/iterators.jl index b78f05c1ae63e..e5df428ea4d08 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -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) diff --git a/test/iterators.jl b/test/iterators.jl index 44538ba5058bc..02d99039b62b4 100644 --- a/test/iterators.jl +++ b/test/iterators.jl @@ -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]