You can also use InitialValues.jl together with BangBang.jl
using InitialValues
using BangBang
g(x) = x*x
function f(x)
res = InitialValue(push!!)
for el in x
res = push!!(res, g(el))
end
return res
end
with the following result
julia> x1 = [1, 2, 3]
3-element Vector{Int64}:
1
2
3
julia> f(x1)
3-element Vector{Int64}:
1
4
9
julia> x2 = ["a", "b", "c"]
3-element Vector{String}:
"a"
"b"
"c"
julia> f(x2)
3-element Vector{String}:
"aa"
"bb"
"cc"
Be warned though, that the usage of push!! adds small overhead which may be or may be not crucial for you.