Stuck with another allocation question

See this entry in the performance tips: Performance Tips · The Julia Language.

To force specialization on the type, use:

function select(v, ::Type{R}, f::F) where F <: Function where R
    ans = similar(v, R, 0)
    for i in 1:length(v)
        push!(ans, f(v[i]))
    end
    ans
end
3 Likes