Operate only on a few components of a static vector

using StaticArrays
v = SVector( (2i for i in 1:10)...)
indices = (1, 3, 5)
f( (x, y, z) ) = (3x, 4y, 5z)
function foo(f, v::StaticArray, indices)
    v2 = getindex.((v,), indices)
    w = f(v2)
    mv = MArray(v)
    # checked by `getindex`
    @inbounds for (i, idx) in enumerate(indices)
        mv[idx] = w[i]
    end
    return SArray(mv)
end

gives

julia> @btime foo(f, $v, $indices);
  8.646 ns (0 allocations: 0 bytes)
3 Likes