JPhrases/InsideBoxes

From J Wiki
Jump to: navigation, search

13A. Inside Boxes

a0=: each=: &.> Apply verb to each box
m1=: open=: -:> :: 1: Test if open (not boxed)
v2=: fmt=: ": Format
m3=: just=: 3 : '9!:3 (2) [ 9!:17 y.' Set function display to boxed form, and set justification within boxes to y
m4=: L. = 0: Test if open
m5=: < -: {:@;~ Test if open
m6=: 32&>@(3!:0) Test if open
m7=: <^:(L. = 0:) Box if open

The adverb each applies its verb argument to the "inside" of each box of the argument of the resulting function, as illustrated below:

   ]c=: <"1 <"2 i.3 3 3 2
+---------------------------------------------------------+
|+---------------+|+-----------------+|+-----------------+|
||0 1| 6  7|12 13|||18 19|24 25|30 31|||36 37|42 43|48 49||
||2 3| 8  9|14 15|||20 21|26 27|32 33|||38 39|44 45|50 51||
||4 5|10 11|16 17|||22 23|28 29|34 35|||40 41|46 47|52 53||
|+---------------+|+-----------------+|+-----------------+|
+---------------------------------------------------------+

   |. each c
+---------------------------------------------------------+
|+---------------+|+-----------------+|+-----------------+|
||12 13| 6  7|0 1|||30 31|24 25|18 19|||48 49|42 43|36 37||
||14 15| 8  9|2 3|||32 33|26 27|20 21|||50 51|44 45|38 39||
||16 17|10 11|4 5|||34 35|28 29|22 23|||52 53|46 47|40 41||
|+---------------+|+-----------------+|+-----------------+|
+---------------------------------------------------------+

   |. each each c
+---------------------------------------------------------+
|+---------------+|+-----------------+|+-----------------+|
||4 5|10 11|16 17|||22 23|28 29|34 35|||40 41|46 47|52 53||
||2 3| 8  9|14 15|||20 21|26 27|32 33|||38 39|44 45|50 51||
||0 1| 6  7|12 13|||18 19|24 25|30 31|||36 37|42 43|48 49||
|+---------------+|+-----------------+|+-----------------+|
+---------------------------------------------------------+

    |. each each each c
+---------------------------------------------------------------------------+
|+---------------------+|+-----------------------+|+-----------------------+|
||+---+|+-----+|+-----+|||+-----+|+-----+|+-----+|||+-----+|+-----+|+-----+||
|||0|1|||6 |7 |||12|13|||||18|19|||24|25|||30|31|||||36|37|||42|43|||48|49|||
||+-+-||+--+--||+--+--||||+--+--||+--+--||+--+--||||+--+--||+--+--||+--+--|||
|||2|3|||8 |9 |||14|15|||||20|21|||26|27|||32|33|||||38|39|||44|45|||50|51|||
||+-+-||+--+--||+--+--||||+--+--||+--+--||+--+--||||+--+--||+--+--||+--+--|||
|||4|5|||10|11|||16|17|||||22|23|||28|29|||34|35|||||40|41|||46|47|||52|53|||
||+---+|+-----+|+-----+|||+-----+|+-----+|+-----+|||+-----+|+-----+|+-----+||
|+---------------------+|+-----------------------+|+-----------------------+|
+---------------------------------------------------------------------------+

The display of a non-uniform boxed array illustrates the fact that boxed elements are normally justified to the top left. For example:

   ]a=: 2 2$'Baker';'John';('Apt 316','Temple St',:'York, Pa');617 204 4567
+----------------------+
|Baker    |John        |
+---------+------------|
|Apt 316  |617 204 4567|
|Temple St|            |
|York, Pa |            |
+----------------------+

When applied to a boxed right argument, the left argument of the format function specifies the justification within a display, using 0, 1, and 2 for top, centre, bottom, and for left, centre, right:

   fmt=: ":
   1 1 fmt a	Centred rows and columns
+----------------------+
|  Baker  |    John    |
+---------+------------|
|Apt 316  |            |
|Temple St|617 204 4567|
|York, Pa |            |
+----------------------+
   ;/all=: 3 3 #: i. 3 3
+-----------+
|0 0|1 0|2 0|
|0 1|1 1|2 1|
|0 2|1 2|2 2|
+-----------+

   $ all fmt a
3 3 7 25

   <"2 all fmt a
+--------------------------------------------------------------------------+
|+----------------------+|+----------------------+|+----------------------+|
||Baker    |John        ||| Baker  |    John    |||   Baker|        John||
|+---------+------------||+---------+------------||+---------+------------||
||Apt 316  |617 204 4567|||Apt 316  |617 204 4567|||Apt 316  |617 204 4567||
||Temple St|            |||Temple St|            |||Temple St|            ||
||York, Pa |            |||York, Pa |            |||York, Pa |            ||
|+----------------------+|+----------------------+|+----------------------+|
+------------------------+------------------------+------------------------|
|+----------------------+|+----------------------+|+----------------------+|
||Baker    |John        ||| Baker  |    John    |||   Baker|        John||
|+---------+------------||+---------+------------||+---------+------------||
||Apt 316  |            |||Apt 316  |            |||Apt 316  |            ||
||Temple St|617 204 4567|||Temple St|617 204 4567|||Temple St|617 204 4567||
||York, Pa |            |||York, Pa |            |||York, Pa |            ||
|+----------------------+|+----------------------+|+----------------------+|
+------------------------+------------------------+------------------------|
|+----------------------+|+----------------------+|+----------------------+|
||Baker    |John        ||| Baker  |    John    |||   Baker|        John||
|+---------+------------||+---------+------------||+---------+------------||
||Apt 316  |            |||Apt 316  |            |||Apt 316  |            ||
||Temple St|            |||Temple St|            |||Temple St|            ||
||York, Pa |617 204 4567|||York, Pa |617 204 4567|||York, Pa |617 204 4567||
|+----------------------+|+----------------------+|+----------------------+|
+--------------------------------------------------------------------------+

The diagonal elements of this display are also given by the scalar left arguments 0, 1, and 2. Moreover, 9!:16 can be used to set the default justification of boxed displays, as it is in the function just, which first sets the function display to the boxed form. For example:

   totient=: * -.@%@~.&.q:
   just 1

   totient
+-------------------------+
| |+---------------------+|
| ||+-------------+|  |  ||
| |||+------+| |  ||  |  ||
|*||||-.|@|%||@|~.||&.|q:||
| |||+------+| |  ||  |  ||
| ||+-------------+|  |  ||
| |+---------------------+|
+-------------------------+

   just 2

   totient
+-------------------------+
| |+---------------------+|
| ||+-------------+|  |  ||
| |||+------+| |  ||  |  ||
| ||||-.|@|%|| |  ||  |  ||
| |||+------+|@|~.||  |  ||
| ||+-------------+|&.|q:||
|*|+---------------------+|
+-------------------------+