I wrote these to time insertion speed. The first one is scalar, second one performs multiple inserts at once.
insertToVec=: 4 : 0 'idx vec'=. y assert. 1=#idx assert. 1=#x assert. (idx>:_1)*.(idx<:#vec) NB. Insert new item: _1->at end, 0->before start biv=. (0) idx}1$~(#vec)+#idx NB. Boolean insertion vector (x) idx}biv#^:_1]vec ) insertMultiToVec=: 4 : 0 'idx vec'=. y NB. Allow multiple simultaneous inserts assert. (#idx)<:>:#vec assert. ((#x)=#idx)+.1=#x NB. Scalar extension assert. (idx>:_1)*.(idx<:#vec) NB. Insert new item: _1->at end, 0->before start biv=. (0) idx}1$~(#vec)+#idx NB. Boolean insertion vector (x) idx}biv#^:_1]vec ) On Tue, Jan 19, 2021 at 11:56 AM Raul Miller <[email protected]> wrote: > insertbefore=:{{ > 'a b'=. y > (x{.b),a,x}.b > }} > > insertafter=: {{ (x+1) insertbefore y }} > > 2 insertafter "1 [ 10;"1 n > 0 1 2 10 3 4 > 5 6 7 10 8 9 > > FYI, > > -- > Raul > > On Tue, Jan 19, 2021 at 11:51 AM Skip Cave <[email protected]> > wrote: > > > > How does one design a verb that inserts specific elements into a vector > or > > matrix? > > > > For example, an "insertafter" verb would take one left argument, which is > > the index of the array that the element is to be inserted after. The > right > > argument would be two boxed nouns. The first boxed noun is the set to be > > inserted. The second boxed noun is the destination of the insertion. > > > > 2 insertafter 10;1 2 3 4 5 > > > > 1 2 3 10 4 5 > > 2 insertafter 10 11;1 2 3 4 5 > > 1 2 3 10 11 4 5 > > > > Also may need insertbefore: > > 2 insertbefore 10;1 2 3 4 5 > > > > 1 2 10 3 4 5 > > 2 insertbefore 10 11;1 2 3 4 5 > > 1 2 10 11 3 4 5 > > > > Higher ranks? > > > > ]n=.2 5$i.10 > > > > 0 1 2 3 4 > > > > 5 6 7 8 9 > > > > 2 insertafter "1 [ 10;n > > > > 0 1 2 10 3 4 > > > > 5 6 7 10 8 9 > > > > > > What would the code for insertbefore verb look like? > > > > > > Skip Cave > > Cave Consulting LLC > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- Devon McCormick, CFA Quantitative Consultant ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
