Learning J – Part II

2007-04-11

See Part I for essential background on arrays.

The sub-arrays of an array of rank N are called cells. A k-cell is an array of rank k that is part of a (usually) larger dimensional array. An N-dimensional array can be simultaneously regarded as being an N-dimensional array of 0-cells (atoms), an N-1-dimensional array of 1-cells, an N-2-dimensional array of 2-cells, and so on. Thus a 2 x 3 x 4 array can be regarded as a 2 x 3 x 4 array of 0-cells (scalars), a 2 x 3 array of 1-cells (each of which is a length 4 vector in this case), a length 2 vector of 2-cells, or the entire array can be regarded as a 3-cell. The cell is defined to be something sensible even when k > N, namely the whole array.

The shape of the k-cells of an array is the rightmost k items of the shape of the array.

The k-frame of an array is the leftmost part of the array’s shape when the shape of the k-cells is taken away.

When k is N-1 then the array is considered as a vector. In this case the k-cells are called the items of the array.

This rank of a verb governs what cells of a noun the verb applies to. A verb of rank r maps over all the r-cells of a noun. The result is an array of the shape noun’s r-frame. The result is an array of whatever the result of applying the verb to each r-cell is.

The k-cell terminology is useful in explaining the behaviour of J programs. It is also useful when we change the rank of a verb.

The monadic verb i. produces a 0-based sequence of integers in the shape of its (right) operand. i. 3 ⇒ 0 1 2. It works with higher ranks too:

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

12 13 14 15
16 17 18 19
20 21 22 23

The monadic verb {. returns the first item of its (right) operand; it’s called head for this reason. Ordinarily {. has rank infinity, denoted in J by _, meaning it gets applied to the entirety of its operand:

   {. 9 1 1  NB. simple use of {.
9
   i. 2 3  NB. an array of shape 2 3
0 1 2
3 4 5
   {. i. 2 3  NB. returns first item of the array, which is a vector of length 3
0 1 2

We can make {. apply to cells of a smaller rank by using the " conjunction. {."k makes a new verb that is like {. but has rank k. So if we apply {."1 to i. 2 3 (shape 2 3) the result will have a shape of the 1-frame of i. 2 3, that is, shape 2. The contents will be the heads of the 1-cells of i. 2 3:

   {."1 i. 2 3
0 3

Next up: syntax.

One Response to “Learning J – Part II”

  1. glorkspangle Says:

    The ” conjunction.

    My head asplode, again.


Leave a comment