# Preallocate an array with the element type, which is unknow before running?

**URL:** <https://discourse.julialang.org/t/preallocate-an-array-with-the-element-type-which-is-unknow-before-running/85162>\
**Category:** New to Julia\
**Created:** [August 2, 2022, 12:24pm UTC](https://discourse.julialang.org/t/preallocate-an-array-with-the-element-type-which-is-unknow-before-running/85162 "2022-08-02T12:24:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brian1/32/49250_2.png) [@Brian1](https://discourse.julialang.org/u/Brian1)\
**Post date:** [August 2, 2022, 12:24pm UTC](https://discourse.julialang.org/t/preallocate-an-array-with-the-element-type-which-is-unknow-before-running/85162/1 "2022-08-02T12:24:06Z")

</div>

In some situation, I need to preallocate an array, but the element type is not fixed until running.  
Here is a example:

```julia
struct Test{T}
    a::T #some function
    n::Int
end

function (test::Test)(data::AbstractArray)
    res=Array{Float64}(undef,size(data))
    for i = 1 : size(data,1)
        start_i = i < test.n ? 1 : i - test.n + 1
        res[i]=test.a(data[start_i : i])
    end
    res
end

test1=Test(mean,5)
test2=Test(cov,5)

data1=randn(10)
10-element Vector{Float64}:
 -0.59236859043663
  1.1109575552202715
  0.7317098287660007
  0.34954035787008153
 -0.8971785589659997
  0.10284823337677379
  1.1058285220935977
  0.35331116031440585
  1.3334401397129447
 -0.4044996149070988

data1=randn(10,3)

```

`test1(data1)` can running well, but `test2(data2)` not. Because `cov` return `Matrix`. I have to define this:

```julia
function (test::Test{typeof(cov),Int})(data::AbstractArray)
    res=Array{Matrix}(undef,size(data))
    for i = 1 : size(data,1)
        start_i = i < test.n ? 1 : i - test.n + 1
        res[i]=test.a(data[start_i : i])
    end
    res
end

```

But this would generate repeated code. So how can I avoid this?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 2, 2022, 12:51pm UTC](https://discourse.julialang.org/t/preallocate-an-array-with-the-element-type-which-is-unknow-before-running/85162/2 "2022-08-02T12:51:41Z")

</div>

You can make use of `map` instead of the manual loop, that way you do not have to preallocate yourself.

For example:

```julia
function (test::Test)(data::AbstractArray)
    map(1 : size(data,1)) do i
        start_i = i < test.n ? 1 : i - test.n + 1
        test.a(data[start_i : i])
    end
end

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [August 2, 2022, 12:53pm UTC](https://discourse.julialang.org/t/preallocate-an-array-with-the-element-type-which-is-unknow-before-running/85162/3 "2022-08-02T12:53:23Z")

</div>

If you really want to use the loop, you could call the function on the first element of the array and use the type of the result to allocate the array. Then you’d loop over the second to last elements. Using `map` is much more convenient and cleaner though.
