From 16022acd45703772f62ca9ed9f8d8ef9288be9b5 Mon Sep 17 00:00:00 2001 From: Marshall Lochbaum Date: Tue, 21 Jul 2020 15:45:25 -0400 Subject: Custom array formatting in the markdown converter --- docs/doc/depth.html | 127 +++++++++++++++------------------------------------- 1 file changed, 37 insertions(+), 90 deletions(-) (limited to 'docs/doc/depth.html') diff --git a/docs/doc/depth.html b/docs/doc/depth.html index 92e05dd3..e141585c 100644 --- a/docs/doc/depth.html +++ b/docs/doc/depth.html @@ -64,128 +64,75 @@

Functions such as Take and Drop use a single number per axis. When the left argument is a list of numbers, they apply to initial axes. But for convenience, a single number is also accepted, and applied to the first axis only. This is equivalent to ravelling the left argument before applying the function.

    27777"abc"
-2‿7‿7‿7
+⟨ 2 7 7 7 ⟩
     2117777"abc"
-2‿1‿1‿7
+⟨ 2 1 1 7 ⟩
 

In these cases the flexibility seems trivial because the left argument has depth 1 or 0: it is an array or isn't, and it's obvious what a plain number should do. But for the second row in the table, the left argument is always an array. The general case is that the left argument is a vector and its elements correspond to right argument axes:

    32,141  67
-┌───┬───┬───┐
-│3‿1│3‿4│3‿1│
-├───┼───┼───┤
-│2‿1│2‿4│2‿1│
-└───┴───┴───┘
+┌─                         
+╵ ⟨ 3 1 ⟩ ⟨ 3 4 ⟩ ⟨ 3 1 ⟩  
+  ⟨ 2 1 ⟩ ⟨ 2 4 ⟩ ⟨ 2 1 ⟩  
+                          ┘
 

This means the left argument is homogeneous of depth 2. What should an argument of depth 1, or an argument that contains non-arrays, do? One option is to continue to require the left argument to be a list, and convert any non-array argument into an array by enclosing it:

    32,1 <(0=≡)¨ 67
-┌───┬───┐
-│3‿1│2‿1│
-└───┴───┘
+⟨ ⟨ 3 1 ⟩ ⟨ 2 1 ⟩ ⟩
 

While very consistent, this extension represents a small convenience and makes it difficult to act on a single axis, which for Replicate and Group is probably the most common way the primitive is used:

    32123 / "abcde"
-aaabbcddeee
+"aaabbcddeee"
 

With the extension above, every case like this would have to use </ instead of just /. BQN avoids this difficulty by testing the left argument's depth. A depth-1 argument applies to the first axis only, giving the behavior above.

For Select, the depth-1 case is still quite useful, but it may also be desirable to choose a single cell using a list of numbers. In this case the left argument depth can be increased from the bottom using <¨.

    214 <¨ 3452
-┌───────┬───────┐
-│2‿1‿4‿0│2‿1‿4‿1│
-└───────┴───────┘
+⟨ ⟨ 2 1 4 0 ⟩ ⟨ 2 1 4 1 ⟩ ⟩
 

The Depth modifier

The Depth 2-modifier () is a generalization of Each that allows diving deeper into an array. To illustrate it we'll use a shape 43 array of lists of lists.

     n  <12 4322⥊↕48
-┌─────────────┬─────────────┬─────────────┐
-│┌───┬───┐    │┌───┬───┐    │┌───┬─────┐  │
-││0‿1│2‿3│    ││4‿5│6‿7│    ││8‿9│10‿11│  │
-│└───┴───┘    │└───┴───┘    │└───┴─────┘  │
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││12‿13│14‿15│││16‿17│18‿19│││20‿21│22‿23││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││24‿25│26‿27│││28‿29│30‿31│││32‿33│34‿35││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││36‿37│38‿39│││40‿41│42‿43│││44‿45│46‿47││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-└─────────────┴─────────────┴─────────────┘
+┌─                                                                         
+╵ ⟨ ⟨ 0 1 ⟩ ⟨ 2 3 ⟩ ⟩     ⟨ ⟨ 4 5 ⟩ ⟨ 6 7 ⟩ ⟩     ⟨ ⟨ 8 9 ⟩ ⟨ 10 11 ⟩ ⟩    
+  ⟨ ⟨ 12 13 ⟩ ⟨ 14 15 ⟩ ⟩ ⟨ ⟨ 16 17 ⟩ ⟨ 18 19 ⟩ ⟩ ⟨ ⟨ 20 21 ⟩ ⟨ 22 23 ⟩ ⟩  
+  ⟨ ⟨ 24 25 ⟩ ⟨ 26 27 ⟩ ⟩ ⟨ ⟨ 28 29 ⟩ ⟨ 30 31 ⟩ ⟩ ⟨ ⟨ 32 33 ⟩ ⟨ 34 35 ⟩ ⟩  
+  ⟨ ⟨ 36 37 ⟩ ⟨ 38 39 ⟩ ⟩ ⟨ ⟨ 40 41 ⟩ ⟨ 42 43 ⟩ ⟩ ⟨ ⟨ 44 45 ⟩ ⟨ 46 47 ⟩ ⟩  
+                                                                          ┘
      n
 3
 

Reversing n swaps all the rows:

     n
-┌─────────────┬─────────────┬─────────────┐
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││36‿37│38‿39│││40‿41│42‿43│││44‿45│46‿47││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││24‿25│26‿27│││28‿29│30‿31│││32‿33│34‿35││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││12‿13│14‿15│││16‿17│18‿19│││20‿21│22‿23││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌───┬───┐    │┌───┬───┐    │┌───┬─────┐  │
-││0‿1│2‿3│    ││4‿5│6‿7│    ││8‿9│10‿11│  │
-│└───┴───┘    │└───┴───┘    │└───┴─────┘  │
-└─────────────┴─────────────┴─────────────┘
+┌─                                                                         
+╵ ⟨ ⟨ 36 37 ⟩ ⟨ 38 39 ⟩ ⟩ ⟨ ⟨ 40 41 ⟩ ⟨ 42 43 ⟩ ⟩ ⟨ ⟨ 44 45 ⟩ ⟨ 46 47 ⟩ ⟩  
+  ⟨ ⟨ 24 25 ⟩ ⟨ 26 27 ⟩ ⟩ ⟨ ⟨ 28 29 ⟩ ⟨ 30 31 ⟩ ⟩ ⟨ ⟨ 32 33 ⟩ ⟨ 34 35 ⟩ ⟩  
+  ⟨ ⟨ 12 13 ⟩ ⟨ 14 15 ⟩ ⟩ ⟨ ⟨ 16 17 ⟩ ⟨ 18 19 ⟩ ⟩ ⟨ ⟨ 20 21 ⟩ ⟨ 22 23 ⟩ ⟩  
+  ⟨ ⟨ 0 1 ⟩ ⟨ 2 3 ⟩ ⟩     ⟨ ⟨ 4 5 ⟩ ⟨ 6 7 ⟩ ⟩     ⟨ ⟨ 8 9 ⟩ ⟨ 10 11 ⟩ ⟩    
+                                                                          ┘
 

Depth ¯1 is equivalent to Each, and reverses the larger vectors, while depth ¯2 applies Each twice to reverse the smaller vectors:

    ¯1 n
-┌─────────────┬─────────────┬─────────────┐
-│┌───┬───┐    │┌───┬───┐    │┌─────┬───┐  │
-││2‿3│0‿1│    ││6‿7│4‿5│    ││10‿11│8‿9│  │
-│└───┴───┘    │└───┴───┘    │└─────┴───┘  │
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││14‿15│12‿13│││18‿19│16‿17│││22‿23│20‿21││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││26‿27│24‿25│││30‿31│28‿29│││34‿35│32‿33││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││38‿39│36‿37│││42‿43│40‿41│││46‿47│44‿45││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-└─────────────┴─────────────┴─────────────┘
+┌─                                                                         
+╵ ⟨ ⟨ 2 3 ⟩ ⟨ 0 1 ⟩ ⟩     ⟨ ⟨ 6 7 ⟩ ⟨ 4 5 ⟩ ⟩     ⟨ ⟨ 10 11 ⟩ ⟨ 8 9 ⟩ ⟩    
+  ⟨ ⟨ 14 15 ⟩ ⟨ 12 13 ⟩ ⟩ ⟨ ⟨ 18 19 ⟩ ⟨ 16 17 ⟩ ⟩ ⟨ ⟨ 22 23 ⟩ ⟨ 20 21 ⟩ ⟩  
+  ⟨ ⟨ 26 27 ⟩ ⟨ 24 25 ⟩ ⟩ ⟨ ⟨ 30 31 ⟩ ⟨ 28 29 ⟩ ⟩ ⟨ ⟨ 34 35 ⟩ ⟨ 32 33 ⟩ ⟩  
+  ⟨ ⟨ 38 39 ⟩ ⟨ 36 37 ⟩ ⟩ ⟨ ⟨ 42 43 ⟩ ⟨ 40 41 ⟩ ⟩ ⟨ ⟨ 46 47 ⟩ ⟨ 44 45 ⟩ ⟩  
+                                                                          ┘
     ¯2 n
-┌─────────────┬─────────────┬─────────────┐
-│┌───┬───┐    │┌───┬───┐    │┌───┬─────┐  │
-││1‿0│3‿2│    ││5‿4│7‿6│    ││9‿8│11‿10│  │
-│└───┴───┘    │└───┴───┘    │└───┴─────┘  │
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││13‿12│15‿14│││17‿16│19‿18│││21‿20│23‿22││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││25‿24│27‿26│││29‿28│31‿30│││33‿32│35‿34││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-├─────────────┼─────────────┼─────────────┤
-│┌─────┬─────┐│┌─────┬─────┐│┌─────┬─────┐│
-││37‿36│39‿38│││41‿40│43‿42│││45‿44│47‿46││
-│└─────┴─────┘│└─────┴─────┘│└─────┴─────┘│
-└─────────────┴─────────────┴─────────────┘
+┌─                                                                         
+╵ ⟨ ⟨ 1 0 ⟩ ⟨ 3 2 ⟩ ⟩     ⟨ ⟨ 5 4 ⟩ ⟨ 7 6 ⟩ ⟩     ⟨ ⟨ 9 8 ⟩ ⟨ 11 10 ⟩ ⟩    
+  ⟨ ⟨ 13 12 ⟩ ⟨ 15 14 ⟩ ⟩ ⟨ ⟨ 17 16 ⟩ ⟨ 19 18 ⟩ ⟩ ⟨ ⟨ 21 20 ⟩ ⟨ 23 22 ⟩ ⟩  
+  ⟨ ⟨ 25 24 ⟩ ⟨ 27 26 ⟩ ⟩ ⟨ ⟨ 29 28 ⟩ ⟨ 31 30 ⟩ ⟩ ⟨ ⟨ 33 32 ⟩ ⟨ 35 34 ⟩ ⟩  
+  ⟨ ⟨ 37 36 ⟩ ⟨ 39 38 ⟩ ⟩ ⟨ ⟨ 41 40 ⟩ ⟨ 43 42 ⟩ ⟩ ⟨ ⟨ 45 44 ⟩ ⟨ 47 46 ⟩ ⟩  
+                                                                          ┘
 

While a negative depth tells how many levels to go down, a non-negative depth gives the maximum depth of the argument before applying the left operand. On a depth-3 array like above, depth 2 is equivalent to ¯1 and depth 1 is equivalent to ¯2. A depth of 0 means to loop until non-arrays are reached, that is, apply pervasively, like a scalar function.

    'a',"bc" 0 23,4
-┌─────────────┬─────────────┐
-│┌─────┬─────┐│┌─────┬─────┐│
-││'a'‿2│'a'‿3│││'b'‿4│'c'‿4││
-│└─────┴─────┘│└─────┴─────┘│
-└─────────────┴─────────────┘
+┌─                                                 
+· ⟨ ⟨ 'a' 2 ⟩ ⟨ 'a' 3 ⟩ ⟩ ⟨ ⟨ 'b' 4 ⟩ ⟨ 'c' 4 ⟩ ⟩  
+                                                  ┘
 

With a positive operand, Depth doesn't have to use the same depth everywhere. Here, Length is applied as soon as the depth for a particular element is 1 or less, including if the argument has depth 0. For example, it maps over 2,3,4⟩⟩, but not over 11,12, even though these are elements of the same array.

    1 1,2,3,4⟩⟩,5,6,7,8,9,10⟩⟩,11,12⟩⟩
-┌─┬───┬─────┬─┐
-│1│1‿2│1‿2‿3│2│
-└─┴───┴─────┴─┘
+⟨ 1 ⟨ 1 2 ⟩ ⟨ 1 2 3 ⟩ 2 ⟩
 
-- cgit v1.2.3