Insert adverb

From J Wiki
Jump to navigation Jump to search


<=   =>

A / is referred to as insert if it is applied to a verb and the derived verb is then used monadically. The derived verb applies itself monadically by inserting the original verb between the items of the argument.

   sumover =. +/


The adverb / takes the verb argument on its left, which is +, and creates a new verb named sumover.

   sumover 7 5 10
22


The items of the argument 7 5 10 are the three atoms 7, 5, and 10 and the definition of sumover is that it inserts its original verb between the items of the argument.

   7 + 5 + 10
22
   sumover i. 8
28


What if you do this to a table?

   a =. i. 2 3
   a
0 1 2
3 4 5
   sumover a
3 5 7


Interesting, but let's take a closer look. The items of a are the two lists 0 1 2 and 3 4 5. The verb sumover is defined to put the + (the original argument of /) between the items of its argument.

   0 1 2 + 3 4 5
3 5 7


What if there were more rows?

   a=. i. 3 4
   a
0 1  2  3
4 5  6  7
8 9 10 11
   sumover a
12 15 18 21


The items of a are the three lists and with the + inserted between them you have:

   0 1 2 3 + 4 5 6 7 + 8 9 10 11
12 15 18 21


The verb sumover applied to a table gives the sum over the columns. What if you wanted the sum over the rows?

   sumover"1 a
6 22 38


The above is worth thinking about. First give your new verb a name to make it easier to talk about.

   sumrows =. sumover"1
   sumrows a
6 22 38


Look up / in NuVoc and note that the rank information gives the rank of the derived verb. So, sumover has monadic rank _ (unbounded). The verb sumover applies to its entire argument and so inserts its original verb of + between the items of the argument.

The verb sumrows has monadic rank 1 and applies to the 1-cells of its argument. It is applied to each of the 1-cells of the argument, giving a partial result, and these partial results are then assembled into the result frame. Instead of the entire argument being fed to the verb sumrows, 1-cells are fed in, so sumrows inserts + between the items of the 1-cells. The 1-cells of the table argument are the rows of the table, so the + is inserted between the items of the rows. Visually:

sumrows   0 1 2 3	(first 1-cell)	gives  6
sumrows   4 5 6 7	(next 1-cell)	gives  22
sumrows   8 9 10 11	(next 1-cell)	gives  38


The partial results of 6, 22, and 38 are then assembled into the list result.

What about a rank 3 argument?

   a =. i. 2 3 4
   a
 0  1  2  3
 4  5  6  7
 8  9 10 11

12 13 14 15
16 17 18 19
20 21 22 23
   sumover a
12 14 16 18
20 22 24 26
28 30 32 34


The items are the two tables and putting the + between them gives the result. Because this is the sum over the items, and in this case is the sum over the tables of a rank 3 array, it can be described as the sum over the planes of the array.

The name sumover was used because it made it clearer in the beginning what was being done. In practice it is probably better to just use the primitives directly.

   a =. i. 2 3 4
   +/ a	NB. sum over planes
12 14 16 18
20 22 24 26
28 30 32 34
   +/"2 a	NB. sum over tables
12 15 18 21
48 51 54 57
   +/"1 a	NB. sum over rows
 6 22 38
54 70 86


<=   =>

Primer Index               Hover to reveal titles   -   Click to access   -   Current page is highlighted
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
45 46 47 48
50 51 52 53 54 55 56 57
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
97 98 99 100 101 102 103 104 105 106