Initiating a Tuple of Arrays

Dear all,

I would like to initiate a Tuple of Vectors. I usually use map in this case as the return type is often of type Tuple, but for some reason the following MWE outputs a Vector of Vector instead of a Tuple of Vectors:

function initvec(T, sz)
        return Vector{T}(undef, sz)

function tuplevecs(T, inner, outer)
        return map(iter -> initvec(T, inner), 1:outer)

initvec(Float64, 10) #Vector{Float}
tupvec = tuplevecs(Float64, 10, 100) #Vector{Vector{Float64}}
typeof(tupvec) #Vector{Vector{Float64}}

I can manually change the type via Tuple(tupvec), but that seems inefficient.

Question: Is there a way to initialize a Tuple of Vectors via map? It does not really have to be map based either if there is another clever way to do what I intend to do.

Have you checked ntuple?

inner = 10; outer = 100
tupvec2 = ntuple( _ -> initvec(Float64,inner), outer)

julia> typeof(tupvec2)
NTuple{100, Vector{Float64}}

This works perfect, thank you!

From what I understand, to get a Tuple you need to call map with a Tuple. Example:

julia> map(i -> Vector{Bool}(undef, 2), 1:3) # map called with AbstractVector
3-element Vector{Vector{Bool}}:
 [0, 0]
 [0, 0]
 [0, 0]

julia> map(i -> Vector{Bool}(undef, 2), (1, 2, 3)) # map called with Tuple
(Bool[1, 0], Bool[0, 0], Bool[0, 0])

Yeah, I’m a bit curious why @mrVeng expected tuple output in the first place. Maybe there’s some misunderstanding?

It totally makes sense now, but I did not know that the return type is chosen based on the iterator.

Thank you, that explains everything really well!