Stream: helpdesk (published)

Topic: How to view a Vector with a NTuple?


view this post on Zulip Júlio Hoffimann (Feb 08 2021 at 16:25):

Given v = [1,2,3,4,5] and inds = (1,2,3), how do you produce the view view(v, inds) without allocating memory?

Currently I am collecting the tuple into a vector with view(v, collect(inds)) but that seems unnecessary?

view this post on Zulip Michael Abbott (Feb 08 2021 at 16:36):

I am slightly surprised this works, but here's a concatenation of zero-dimensional views:

using LazyStack; stack(map(i -> view(v, i), inds))

This should also be zero-allocations:

using StaticArrays; view(v, SVector(inds...))

view this post on Zulip Júlio Hoffimann (Feb 08 2021 at 17:05):

The StaticArrays.jl solution is the best in my use case that already depends on the package. I wonder if this should work with Tuple too? Perhaps an issue should be opened?

view this post on Zulip Mason Protter (Feb 08 2021 at 17:05):

Yeah that seems like a reasonable thing to support in base


Last updated: Nov 06 2024 at 04:40 UTC