Each

From J Wiki
Jump to navigation Jump to search


<=   =>

Frequently with boxed data it is useful to be able to do something to the contents of each of the boxes. This is so useful that the standard profile defines an adverb called each that does exactly this. The definition of each involves a little more than you have covered so far, but don't worry about the details, just use it.

The adverb each takes a verb as its left argument. The derived verb is applied to the contents of the boxes of its arguments.

   a =. 10 12 13 ; 2 3 ; 4 5 6 8 3
   a
+--------+---+---------+
|10 12 13|2 3|4 5 6 8 3|
+--------+---+---------+
   +/ each a	NB. sum over each
+--+-+--+
|35|5|26|
+--+-+--+
   */ each a	NB. times over each
+----+-+----+
|1560|6|2880|
+----+-+----+
   |. each a	NB. reverse each
+--------+---+---------+
|13 12 10|3 2|3 8 6 5 4|
+--------+---+---------+
   >./ each a	NB. max over each
+--+-+-+
|13|3|8|
+--+-+-+


The previous examples all used the derived verb monadically. The following use the derived verb dyadically.

   23 , each a		NB. append each
+-----------+------+------------+
|23 10 12 13|23 2 3|23 4 5 6 8 3|
+-----------+------+------------+
   5 < each a
+-----+---+---------+
|1 1 1|0 0|0 0 1 1 0|
+-----+---+---------+
   1 |. each a
+--------+---+---------+
|12 13 10|3 2|5 6 8 3 4|
+--------+---+---------+


Did you catch the new verb |. (reverse or rotate) that slipped in above? Did you look it up in NuVoc?

<=   =>

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