Home:ALL Converter>Inserting an element into an array in J

Inserting an element into an array in J

Ask Time:2017-07-30T02:09:28         Author:cole

Json Formatter

What is the best practice for inserting an element into an array at an arbitrary position in J?

I guess this is sort of a double question: my main issue is figuring out how to provide three arguments to the verb I want to create. The gist of the code that I want to write is

insert =. dyad : '(n {. y) , x , (n }. y)'

for a position n. The best solution to this that I can think of is taking a two-length array of boxes as the right argument and the position as the left, but that seems a bit clunky

insert =. dyad : 0
       NB. the array to be inserted is the first argument
       i =. > {. y
       NB. the original array is the second argument
       a =. > {: y
       (x {. a) , i , (x }. a)
)

EDIT: Furthermore, would it be possible to take an array of indices to insert the item at and an array of items to be inserted at those indices -- i.e. inserting multiple items at a time? It seems to me like this is something J would be good at, but I'm not sure how it would be done.

Author:cole,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/45392474/inserting-an-element-into-an-array-in-j
yy