# How to efficiently transform vector of structures?

How to efficiently transform vector of structures without unnecessary allocation? We have two structures:

``````struct ApiData
name::String
values::Vector{Float64}
end

status::String
time::Sting  #Transform to sturcture "TimeOfDay"
end
``````

which make the vector a = `Vector{ApiData}`. Now we want to transform the vector a into vector b which has the form of three structures:

``````struct Data
name::String
values::Vector{Float64}
end

status::String
time::TimeOfDay
end

struct TimeOfDay
minutes::Array{Int}
hours::Array{Int}
end
``````

Vector b has the form of `Vector{Data}`, with percentage members in the `Data` structure (name =1%, reload=4% and values 95%).

What have you tried and what do you mean with unnecessary allocation? Wouldn’t it be sufficient to preallocate the vector b and fill it in a loop?

@laborg In fact, I have a third structure, yet. For C API.

``````struct C_ApiData
name::Ptr{Cwchar_t}
valuesLength::Csize_t
values::Vector{Cdouble}
end
``````

And I’m looking for the most optimal way (design Pattern).

You haven’t specified how fields should be transformed, so I’m just guessing on the similarity of type names, but then its not clear how `ApiReload` with a single `String` should map to `Reload` having `Array{Int}` for minutes and hours.

Now, looking at your last line:

`Vector{Data}` , with percentage members in the `Data` structure…

should the transform contain any calculations as well, or is it merely a structural transformation?

Yes, there will be a `parse` function that transforms `String` to structure `TimeOfDay`. Further e.g. `a.reload.status` to ` enum`

@enum Status lunch=1 evening=2

etcetera …

I’d go with:

``````parsetime(t::String) = TimeOfDay(,)
transform(x::ApiData) = Data(x.name, transform.(x.reload), x.values)

a = ApiData[
ApiData("ap1",
Float64[1.,2.,3.,4.]),
ApiData("ap2",