From b363cc70ee6f09bb4472944f8690283793c6052a Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 6 Jul 2016 00:20:20 -0400 Subject: [PATCH] shorten call chain for `collect(::Generator)` --- base/array.jl | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/base/array.jl b/base/array.jl index 81e0cc2866fc5..2a19d201cabda 100644 --- a/base/array.jl +++ b/base/array.jl @@ -254,6 +254,24 @@ else end _default_eltype{I,T}(::Type{Generator{I,Type{T}}}) = T +_array_for(T, itr, ::HasLength) = Array(T, Int(length(itr)::Integer)) +_array_for(T, itr, ::HasShape) = Array(T, convert(Dims,size(itr))) + +function collect(itr::Generator) + isz = iteratorsize(itr.iter) + et = _default_eltype(typeof(itr)) + if isa(isz, SizeUnknown) + return grow_to!(Array(et, 0), itr) + else + st = start(itr) + if done(itr,st) + return _array_for(et, itr.iter, isz) + end + v1, st = next(itr, st) + collect_to_with_first!(_array_for(typeof(v1), itr.iter, isz), v1, itr, st) + end +end + _collect(c, itr, ::EltypeUnknown, isz::SizeUnknown) = grow_to!(_similar_for(c, _default_eltype(typeof(itr)), itr, isz), itr)