# Collect (flat) iterator as an Array

**URL:** <https://discourse.julialang.org/t/collect-flat-iterator-as-an-array/108617>\
**Category:** General Usage\
**Tags:** question, iterators\
**Created:** [January 10, 2024, 12:24pm UTC](https://discourse.julialang.org/t/collect-flat-iterator-as-an-array/108617 "2024-01-10T12:24:38Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 10, 2024, 12:24pm UTC](https://discourse.julialang.org/t/collect-flat-iterator-as-an-array/108617/1 "2024-01-10T12:24:38Z")

</div>

Is there a _default_ generic way I can collect an iterator `itr` that `HasLength` into an `Array` of given (compatible) `dims`?

Eg collect

```julia
itr = (i for i in 1:6)

```

into a 2x3 `Matrix`.

Just to clarify, it is easy to write a wrapper that provides the given size. I am asking if there is a package with this functionality, or if I can somehow do it in base.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 10, 2024, 12:28pm UTC](https://discourse.julialang.org/t/collect-flat-iterator-as-an-array/108617/2 "2024-01-10T12:28:07Z")

</div>

And just to clarify, I am not looking for `reshape(collect(itr), (2,3))`, but a generic mechanism that endows an iterator with a given shape.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [January 10, 2024, 12:45pm UTC](https://discourse.julialang.org/t/collect-flat-iterator-as-an-array/108617/3 "2024-01-10T12:45:37Z")

</div>

Could do something like

```julia
struct ShapedIterator{I, N}
    itr::I
    shape::NTuple{N, Int}
end 
shaped(itr, shape...) = let l = prod(shape)
    l == length(itr) || error("something informative")
    ShapedIterator(itr, shape)
end

Base.IteratorSize(s::ShapedIterator{<:Any, N}) where {N} = Base.HasShape{N}()
Base.IteratorEltype(s::ShapedIterator) = Base.IteratorEltype(s.itr)
Base.eltype(s::ShapedIterator) = eltype(s.itr)
Base.size(s::ShapedIterator) = s.shape
Base.iterate(s::ShapedIterator, args...) = iterate(s.itr, args...)

```

```julia-repl
julia> collect(shaped((i for i in 1:6), 2, 3))
2×3 Matrix{Int64}:
 1 3 5
 2 4 6

```

* * *

**Edit:** sorry I missed that you explicitly said you don’t want to write a wrapper and want to use a package, sorry for the noise.

Maybe this function could be added to IterTools.jl as a PR?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 10, 2024, 12:52pm UTC](https://discourse.julialang.org/t/collect-flat-iterator-as-an-array/108617/5 "2024-01-10T12:52:54Z")

</div>

> [@Mason](#):
>
> sorry I missed

no worries, I should have been more explicit.

> [@Mason](#):
>
> could be added to [IterTools.jl](https://juliahub.com/ui/Packages/IterTools) as a PR?

yes indeed. but I thought I would ask first if a package already has it.
