I am trying to set values at different indexes within a flat vector in once call, e.g. [0 0 0 0] => [0 1 0 3]. I tried
(setval [(nthpath 1)(nthpath 3)] [1 3] [0 0 0 0])
but got a runtime exc….What should I use instead?
Thanks in advance..
@kingcode there's two approaches:
(setval (subselect (multi-path 1 3)) [1 3] [0 0 0 0])
or
(multi-transform
(multi-path
[1 (terminal-val 1)]
[3 (terminal-val 3)])
[0 0 0 0])
Hi Nathan, if I have a vector of vectors with index & new-values,or a vector of maps with index & new-value pairs, then how do I rewrite the above. please advise.. thank you
I am thinking of this data
input vec: [:a :b :c ... :m ... :z]
A map of indices and new values
{
2 :c2
12 :m2
25 :z2
}
Or a map of old value new value pairs
{
:c :c2
:m :m2
:z :z2
}
output vec: [:a :b :c2 ... :m2 ... :z2]
Essentially how to deal with the scenario when I have a lot of values to be replaced and not just a handful of indices that can be hardcoded in the multipath selector expression..
for the latter you can do (transform [ALL #(contains? replacements %)] replacements data)
for the first I would just do that with a reduce
Noted thank you
the second is more efficient
Thank you @nathanmarz!
Looking forward to learn and use more of this great library:)