In J, I can update a subset of an array in place according to some predicate, like so:
y (k}~) |. y {~ k =. I. '123' e.~ y =. '[1.2.3]'
[3.2.1]
I realize I can factor out a conjunction here, but is there a more elegant way to do it in the first place? The 123
is just an example. What I want to do, specificially, is:
- obtain an array of indices into an array (
k
) - extract the items of the array at those indices into a new array
- transform that array into a new array with the same type and shape
- put the values of the new array back into the slots.
Also, it's important that the verb operates on the array as a whole, because what I mainly want to do is permute and transform sub-arrays in place. (Hence the |.
in the example.)
Is there a simpler way to do this?