Vocabulary/AmendingBoxedStructures

From J Wiki
Jump to navigation Jump to search

Back to: Vocabulary

Amending Boxed Structures

J has no primitive verb for making a change deep inside a boxed structure. Some user-written conjunctions and verbs to do so are collected here.

applyintree

u applyintree n applies the verb u to the contents of the cell whose path is n.

Examples of applyintree

   ]y =. (;: 'An array with'), < (;: 'Interesting contents') ; 1 2 3
+--+-----+----+------------------------------+
|An|array|with|+----------------------+-----+|
|  |     |    ||+-----------+--------+|1 2 3||
|  |     |    |||Interesting|contents||     ||
|  |     |    ||+-----------+--------+|     ||
|  |     |    |+----------------------+-----+|
+--+-----+----+------------------------------+
   {. applyintree 2 y
+--+-----+-+------------------------------+
|An|array|w|+----------------------+-----+|
|  |     | ||+-----------+--------+|1 2 3||
|  |     | |||Interesting|contents||     ||
|  |     | ||+-----------+--------+|     ||
|  |     | |+----------------------+-----+|
+--+-----+-+------------------------------+

{. was applied to the contents of the cell with index 2.

   {. applyintree (3;0;1) y
+--+-----+----+-----------------------+
|An|array|with|+---------------+-----+|
|  |     |    ||+-----------+-+|1 2 3||
|  |     |    |||Interesting|c||     ||
|  |     |    ||+-----------+-+|     ||
|  |     |    |+---------------+-----+|
+--+-----+----+-----------------------+

{. was applied to the contents of the cell with path (3;0;1). This path means: start with the overall array, and take item 3 and open it; then take item 0 of those contents, and open that; then take item 1 and open that. The verb u is applied to the final contents.

   2 + applyintree (3;1) y
+--+-----+----+------------------------------+
|An|array|with|+----------------------+-----+|
|  |     |    ||+-----------+--------+|3 4 5||
|  |     |    |||Interesting|contents||     ||
|  |     |    ||+-----------+--------+|     ||
|  |     |    |+----------------------+-----+|
+--+-----+----+------------------------------+

In the dyadic form, the x argument is passed down unchanged until u is executed.

   10 20 + applyintree (3;1;(<<1 2)) y
+--+-----+----+--------------------------------+
|An|array|with|+----------------------+-------+|
|  |     |    ||+-----------+--------+|1 12 23||
|  |     |    |||Interesting|contents||       ||
|  |     |    ||+-----------+--------+|       ||
|  |     |    |+----------------------+-------+|
+--+-----+----+--------------------------------+

Each box in a path follows the rules for left arguments of From (x { y). Here, the double-boxed selector picks 2 values from the last box. 10 20 is added to the pair.

The selection does not have to go all the way to the leaves of the boxed array:

   2 3 ({. each) applyintree (3;0) y
+--+-----+----+----------------+
|An|array|with|+--------+-----+|
|  |     |    ||+--+---+|1 2 3||
|  |     |    |||In|con||     ||
|  |     |    ||+--+---+|     ||
|  |     |    |+--------+-----+|
+--+-----+----+----------------+

The selection stopped at the two-element list of boxes, to which u was applied.

Definition of applyintree

NB. Conjunction.  Apply u at the cell indicated by n
applyintree =: 2 : 0
if. #n do. ((u applyintree (}.n)) L:_1 ({.n){y) ({.n)} y else. u y end.
:
if. #n do. (x u applyintree (}.n) L:_ _1 ({.n){y) ({.n)} y else. x u y end.
)

More modern version with correct support for multidimensional boxing arrays (trees). No support of matching partial value of leaf (double boxing in path).

applyintree =: 2 : 0
  i =. >^:((1=#)@>) {. n
  (u`(u applyintree (}.n))@.(1<#n)&.:> i{y) i} y
:
  i =. >^:((1=#)@>) {. n
  ((x&u)`(x&(u applyintree (}.n)))@.(1<#n)&.:> i{y) i} y
)

Restriction

The shape of the selected portion of y may not change.

   10 , applyintree (3;1;1) y
|rank error
|   (x u applyintree(}.n)L:_ _1({.n){y)    ({.n)}y

The problem is that the modified value replaces the original in the array. Here, we are trying to replace an atom with a list.

Remember, though, that a box is always an atom, so a box can be replaced even if its contents change shape:

   10 , applyintree (3;1) y
+--+-----+----+---------------------------------+
|An|array|with|+----------------------+--------+|
|  |     |    ||+-----------+--------+|10 1 2 3||
|  |     |    |||Interesting|contents||        ||
|  |     |    ||+-----------+--------+|        ||
|  |     |    |+----------------------+--------+|
+--+-----+----+---------------------------------+