Skip to content

Is it possible to map enumeration to SVector? #1106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
drelatgithub opened this issue Nov 5, 2022 · 2 comments
Closed

Is it possible to map enumeration to SVector? #1106

drelatgithub opened this issue Nov 5, 2022 · 2 comments

Comments

@drelatgithub
Copy link

drelatgithub commented Nov 5, 2022

When I do the following:

v = SA[10, 20, 30]
map(x -> x[1] + x[2], enumerate(v))

The result is a 3-element Vector{Int64} of values 11, 22 and 33. Is it possible to get the result in SVector since the length is known at compile time (assuming that the codes above is inside a function, and that v has fixed but unknown length)?

Other methods I tried but not did not work:

  1. I tried to use comprehension and macro @SVector, but it seems that in this case I need length(v) but its value is unknown during macro expansion.
  2. I also tried eachindex(v) but it seems that the result is converted to MVector instead of SVector. This part seems to have been covered in map SOneTo to SVector #1032
@drelatgithub drelatgithub changed the title Is it possible to map enumeration to SVector Is it possible to map enumeration to SVector? Nov 5, 2022
@mateuszbaran
Copy link
Collaborator

Yes, mapping such an enumeration should give an SVector. I will take a look, maybe it won't be hard to implement.

@mateuszbaran
Copy link
Collaborator

Since StaticArrays 1.5.10 it is possible to do static mapping of enumeration using enumerate_static (instead of enumerate):

julia> using StaticArrays

julia> v = SA[10, 20, 30]
3-element SVector{3, Int64} with indices SOneTo(3):
 10
 20
 30

julia> map(x -> x[1] + x[2], enumerate_static(v))
3-element SVector{3, Int64} with indices SOneTo(3):
 11
 22
 33

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants