Vocabulary/curlylfcoco

From J Wiki
Jump to navigation Jump to search

>> <<   Down to: Dyad   Back to: Vocabulary Thru to: Dictionary

{:: y Map

Rank Infinity -- operates on x and y as a whole -- WHY IS THIS IMPORTANT?



A map of the boxed noun y
i.e. a noun having the same box structure as y but with each leaf replaced by the path to that leaf.

  • A leaf is a noun that is unboxed.
    This includes one that is empty, i.e. has no contents.
  • A path is a boxed noun whose leaves are integers.
    It describes a sequence of selections and unboxings that points to a given (nested) box of a boxed noun.
    A valid path can serve as x in Fetch (x {:: y) to fetch the contents of its target box.
   boxxopen_z_   NB. a standard library (stdlib) verb with an interesting structure
<^:(L. < *@#)

   ] y=: 5!:2<'boxxopen_z_'  NB. a sample boxed noun describing its structure
+-+--+--------------+
|<|^:|+--+-+-------+|
| |  ||L.|<|+-+-+-+||
| |  ||  | ||*|@|#|||
| |  ||  | |+-+-+-+||
| |  |+--+-+-------+|
+-+--+--------------+

   {:: y         NB. replaces each LEAF of y by its PATH
+---+---+---------------------------------------+
|+-+|+-+|+-----+-----+-------------------------+|
||0|||1|||+-+-+|+-+-+|+-------+-------+-------+||
|+-+|+-+|||2|0|||2|1|||+-+-+-+|+-+-+-+|+-+-+-+|||
|   |   ||+-+-+|+-+-+|||2|2|0|||2|2|1|||2|2|2||||
|   |   ||     |     ||+-+-+-+|+-+-+-+|+-+-+-+|||
|   |   ||     |     |+-------+-------+-------+||
|   |   |+-----+-----+-------------------------+|
+---+---+---------------------------------------+

   ] x=: 2;2;1   NB. the PATH corresponding to the LEAF: '@'
+-+-+-+
|2|2|1|
+-+-+-+
   x {:: y       NB. Use the PATH (x) to extract the LEAF: '@'
@



Oddities

1. {:: y does not produce a path for a leaf that contains an empty array of boxes.



x {:: y Fetch

Rank 1 _ -- operates on lists of x and the entirety of y -- WHY IS THIS IMPORTANT?


A list of the contents of selected boxes in a boxed noun: y

Each sub-path of x gets replaced by its target box.

If x points to a single box, the result is the contents of that box

   ]y =. 1 2;3 4;5 6 7
+---+---+-----+
|1 2|3 4|5 6 7|
+---+---+-----+

   1 {:: y
3 4

   ] y=: 5!:2<'boxxopen'
+-+--+--------------+
|<|^:|+--+-+-------+|
| |  ||L.|<|+-+-+-+||
| |  ||  | ||*|@|#|||
| |  ||  | |+-+-+-+||
| |  |+--+-+-------+|
+-+--+--------------+

   2 {:: y
+--+-+-------+
|L.|<|+-+-+-+|
|  | ||*|@|#||
|  | |+-+-+-+|
+--+-+-------+

   (2;0) {::y
L.

   (2;2) {::y
+-+-+-+
|*|@|#|
+-+-+-+

   (2;2;1) {::y
@

   (0,:1) {::y   NB. the result has the tally (#) of x  (i.e. 2)
<
^:

When x and y are unboxed, (x {:: y) performs simple indexing into y

   5 6 {:: i. 10 10   NB. Fetch row 5 column 6
56

   (<5 6) { i. 10 10   NB. This is the other way to do that
56

Important.png (x {:: y) fetches portions of a boxed structure. There is no corresponding primitive to modify a portion of a boxed structure. x m}:: y is reserved for that purpose, planned to operate along the lines of x m} y, but it is unimplemented. If you want to modify internal portions of a multilevel boxed noun, look at utilities for amending boxed structures.


Common Uses

1. To select and open a single box from a list of boxes

   ]a =: 'zero';'one';'two';'three'
+----+---+---+-----+
|zero|one|two|three|
+----+---+---+-----+
   2 {:: a
two

For this purpose   x {:: y is faster than the equivalent    > x { y

2. To select from a deep structure

   ]a =: 'zero';'one';('two point zero';'two point one';'two point two');'three'
+----+---+--------------------------------------------+-----+
|zero|one|+--------------+-------------+-------------+|three|
|    |   ||two point zero|two point one|two point two||     |
|    |   |+--------------+-------------+-------------+|     |
+----+---+--------------------------------------------+-----+
   (2;1) {:: a
two point one
   (< < 1 3 _1) {:: a
+---+-----+-----+
|one|three|three|
+---+-----+-----+
   (2 ; < < 1 2) {:: a
+-------------+-------------+
|two point one|two point two|
+-------------+-------------+

3. To perform multidimensional indexing, when x is unboxed.

This is equivalent to    >^:('' -.@-: $) (<x) { y

  • select from y using x
  • if the result is not an atom, open it.
   2 1 3 {:: i. 10 10 10    NB. coordinates for each axis
213

   2 1 {:: i. 10 10 10      NB. omitted trailing axes are taken in full
210 211 212 213 214 215 216 217 218 219

   1 2 {:: i. 3 4           NB. selection of an atom
6

   1 2 {:: <"0 i. 3 4       NB. if the atom is boxed, it is opened
6

   1 2 {:: i. 3 4 5         NB. selection of multiple atoms
30 31 32 33 34

   1 2 {:: <"0 i. 3 4 5     NB. they are not opened
+--+--+--+--+--+
|30|31|32|33|34|
+--+--+--+--+--+

4. To select a subarray from a boxed array

   ]A =: 'abcde' ; 10 + i. 2 4
+-----+-----------+
|abcde|10 11 12 13|
|     |14 15 16 17|
+-----+-----------+

   (0 ; << 1 2 3) {:: A     NB. selection of a subvector
bcd
   (0 ; <<< 1 2 3) {:: A    NB. additional boxing excludes indices
ae
   (1 ; 0 1) {:: A          NB. selection of a scalar from subarray
11
   (1 ; < 0 ; 1) {:: A      NB. the same as above
11

   (1 ; < 0 1 ; 1 2) {:: A  NB. selection of a submatrix
11 12
15 16

More Information

The path is defined by its effect on  x {:: y .

The path is a list of boxes which are processed one by one, as follows:

  1. If x is empty or unboxed, it is put into a single box before the following is executed; that is, unboxed or empty x is processed as <x.
  2. selectedy =: y initializes the noun being searched
  3. Then, for each box of x:
    1. selectedy =: box { selectedy is executed, i.e. the box is used as the selector to select from selectedy
    2. If the selectedy is an atom, it is opened.
   ]a =: 'zero';'one';('two point zero';'two point one');'three'
+----+---+------------------------------+-----+
|zero|one|+--------------+-------------+|three|
|    |   ||two point zero|two point one||     |
|    |   |+--------------+-------------+|     |
+----+---+------------------------------+-----+

   (<0) {:: a     NB. 0 means: item 0 along the first axis
zero

   (<,0) {:: a    NB. ...means the same thing
zero

   (<<0) {:: a    NB. ...means the same thing
zero

   (<<,0) {:: a   NB. ...now it is a list of items (length 1), therefore not unboxed
+----+
|zero|
+----+
   $ (<<,0) {:: a
1

The same rules apply when going down into the structure

   ((<2),(<<0)) {:: a
two point zero
   ((<2),(<,0)) {:: a
two point zero
   ((<2),(<<,0)) {:: a
+--------------+
|two point zero|
+--------------+
   ((<2),(<<0 1)) {:: a
+--------------+-------------+
|two point zero|two point one|
+--------------+-------------+

(;) is often used to build x.

Remember:

  • x;y always boxes x
  • x;y boxes y only if not already boxed
   (2;,0) {:: a
two point zero
   (2;<,0) {:: a
two point zero
   (2;<<,0) {:: a
+--------------+
|two point zero|
+--------------+

Details

1. If x is unboxed, it is first boxed before being used as the path.

   ] z=:  0 1 {:: i. 2 3
1

   assert z -:  (<0 1) {:: i. 2 3

Exception: If x is an atom, it is used as the selector directly. The only difference this makes is that it causes (0 {:: atom) to give a result of (> atom) rather than an error.


2. If any selection other than the last selects an array, J signals rank error .

   ((<<0 1),(<'')) {:: a  NB. Can't follow more than one selection path
|rank error

   ((<<0 1)) {:: a        NB. But may end with multiple leaves
+----+---+
|zero|one|
+----+---+

Oddities

1. The definition of fetch in the J Dictionary does not conform to the behavior of x{::y as described above.