In J many operations that are traditionally grouped into specific libraries (string operations, list operations) are generalized for broader range of arrays and distinction can be blurred between character or numerical data.

The cut u;.n conjunction represents a family of partitioning operations taken by selector n, much like circle x o. functions. As other J traditions go, the subset is not merely returned, but a verb in u is applied to them; also the arguments, indices and subsets are in general with multidimensional arrays in mind.

Ranges u;.0

This is generalization of most common operation substring or more generally, selecting a range from an array.

The range is given in x argument by starting point and extent (size). But because it is generalized to higher arrays, these are given not as scalars but as index and shape vectors. To accomodate this pair we need a 2×N table where N is rank (number of dimensions) of our indices.

   A=. 'abcdefghij'
   (,:~ {:@":"0@i.@#) A     NB. indices of items
0123456789
abcdefghij
   
   (3,:4)  ];.0 A           NB. 4 items from 3
defg

Negative position counts from back index of tail. Negative size reverses result.

   (_3,:4) ];.0 A           NB. 4 items with 3rd from end
efgh
   (3,:_4) ];.0 A           NB. reverse result
gfed

Multiple differently shaped subsets in one operation are possible. The left rank of cut is 2 and normal expansion for higher rank is used.

   ((2,:3),:(3,:4)) <;.0 A  NB. multiple boxed cuts
+---+----+
|cde|defg|
+---+----+

Here is an example with a table.

   ]B=: 4 6$a.{~97+i.24     NB. 4×6 alphabet table
abcdef
ghijkl
mnopqr
stuvwx

Rank of indices gives the frame of operation, cells are complemented to argument rank.

   (1,:2)  ];.0 B           NB. two rows from 1
ghijkl
mnopqr
   (1 2,:2 3)  ];.0 B       NB. 2×3 block from (1,2)
ijk
opq

OlegKobchenko/Cut (last edited 2011-02-23 15:42:38 by DevonMcCormick)