# Iterators.Repeated and Iterators.Take are not AbstractArrays

**URL:** <https://discourse.julialang.org/t/iterators-repeated-and-iterators-take-are-not-abstractarrays/24696>\
**Category:** General Usage\
**Created:** [May 28, 2019, 2:04pm UTC](https://discourse.julialang.org/t/iterators-repeated-and-iterators-take-are-not-abstractarrays/24696 "2019-05-28T14:04:19Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonas-schulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-schulze/32/9228_2.png) [@jonas-schulze](https://discourse.julialang.org/u/jonas-schulze)\
**Post date:** [May 28, 2019, 2:04pm UTC](https://discourse.julialang.org/t/iterators-repeated-and-iterators-take-are-not-abstractarrays/24696/1 "2019-05-28T14:04:19Z")

</div>

I tried to create a bidiagonal matrix using iterators:

```julia
using .Iterators
Bidiagonal(repeated(13, 37), repeated(42), :L)

```

which doesn’t work:

```julia
ERROR: MethodError: no method matching Bidiagonal(::Base.Iterators.Take{Base.Iterators.Repeated{Int64}}, ::Base.Iterators.Repeated{Int64}, ::Symbol)

```

Internally `dv` and `ev` are stored densly, so it wouldn’t harm to `collect()` those iterators. Anyways, why is `repeated(12,3) isa AbstractArray` false?

---

<div class="post-metadata">

**Author:** ![ExpandingMan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/expandingman/32/866_2.png) [@ExpandingMan](https://discourse.julialang.org/u/ExpandingMan)\
**Post date:** [May 28, 2019, 2:10pm UTC](https://discourse.julialang.org/t/iterators-repeated-and-iterators-take-are-not-abstractarrays/24696/2 "2019-05-28T14:10:42Z")

</div>

I suppose it’s because the `AbstractArray` version of that would be `fill(12,3)`.

I do agree that it would be nice to have more non-allocating `AbstractArray` objects for just the sort of initialization that you are showing here.

Even better, it would be nice to have some sort of `array(iter)` function that returns a lazy `AbstractArray` from an interator. Of course, this would only work for iterators for which `length` is defined.

---

<div class="post-metadata">

**Author:** ![jonas-schulze](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonas-schulze/32/9228_2.png) [@jonas-schulze](https://discourse.julialang.org/u/jonas-schulze)\
**Post date:** [May 28, 2019, 6:31pm UTC](https://discourse.julialang.org/t/iterators-repeated-and-iterators-take-are-not-abstractarrays/24696/3 "2019-05-28T18:31:01Z")

</div>

It looks like a lot of that is available within [LazyArrays.jl](https://github.com/JuliaArrays/LazyArrays.jl).

---

<div class="post-metadata">

**Author:** ![mschauer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mschauer/32/13946_2.png) [@mschauer](https://discourse.julialang.org/u/mschauer)\
**Post date:** [May 29, 2019, 11:06am UTC](https://discourse.julialang.org/t/iterators-repeated-and-iterators-take-are-not-abstractarrays/24696/4 "2019-05-29T11:06:46Z")

</div>

`Repeated` doesn’t quite fit into `AbstractVector`, as it has no `length` (it could support `getindex` though) and `Take` can’t be an `AbstractVector` because it cannot support `getindex`).

---

<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:** [May 29, 2019, 11:29am UTC](https://discourse.julialang.org/t/iterators-repeated-and-iterators-take-are-not-abstractarrays/24696/5 "2019-05-29T11:29:19Z")

</div>

> [@ExpandingMan](#):
>
> Even better, it would be nice to have some sort of `array(iter)` function that returns a lazy `AbstractArray` from an interator.

I think that unless there is a compelling reason not to do so, all functions that accept `AbstractVector` should just fall back to trying to `collect` other types, eg

```julia
ensure_vector(v::AbstractVector) = v
ensure_vector(v) = collect(v)

some_function(a::AbstractVector, b::AbstractVector) = ...
some_function(a, b) = some_function(ensure_vector(a), ensure_vector(b))

```
