aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/rank.html
diff options
context:
space:
mode:
Diffstat (limited to 'docs/doc/rank.html')
-rw-r--r--docs/doc/rank.html94
1 files changed, 94 insertions, 0 deletions
diff --git a/docs/doc/rank.html b/docs/doc/rank.html
new file mode 100644
index 00000000..fd618d93
--- /dev/null
+++ b/docs/doc/rank.html
@@ -0,0 +1,94 @@
+<head>
+ <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
+ <link href="../style.css" rel="stylesheet"/>
+ <title>BQN: Cells and Rank</title>
+</head>
+<div class="nav">(<a href="https://github.com/mlochbaum/BQN">github</a>) / <a href="../index.html">BQN</a> / <a href="index.html">doc</a></div>
+<h1 id="cells-and-rank"><a class="header" href="#cells-and-rank">Cells and Rank</a></h1>
+<p>The Cells modifier <code><span class='Modifier'>˘</span></code> applies a function to major cells of its argument, much like <a href="map.html">Each</a> applies to elements. Each result from <code><span class='Function'>𝔽</span></code> becomes a major cell of the result, which means they must all have the same shape.</p>
+<p>The Rank modifier <code><span class='Modifier2'>⎉</span></code> generalizes this concept by allowing numbers provided by <code><span class='Function'>𝔾</span></code> to specify a rank for each argument: non-negative to indicate the rank of each array passed to <code><span class='Function'>𝔽</span></code>, or negative for the number of axes that are mapped over. Cells, which maps over one axis of each argument, is identical to <code><span class='Modifier2'>⎉</span><span class='Number'>¯1</span></code>. Rank is analogous to the <a href="depth.html#the-depth-modifier">Depth modifier</a>, but the homogeneous structure of an array eliminates some tricky edge cases found in Depth.</p>
+<h2 id="cells"><a class="header" href="#cells">Cells</a></h2>
+<p>The function Cells (<code><span class='Modifier'>˘</span></code>) is named after <em>major cells</em> in an array. A major cell is a component of an array with dimension one smaller, so that the major cells of a list are <a href="enclose.html#whats-a-unit">units</a>, the major cells of a rank-2 table are its rows (which are lists), and the major cells of a rank-3 array are tables.</p>
+<p>The function <code><span class='Function'>𝔽</span><span class='Modifier'>˘</span></code> applies <code><span class='Function'>𝔽</span></code> to the major cells of <code><span class='Value'>𝕩</span></code>. So, for example, where <a href="shift.html">Nudge</a> (<code><span class='Function'>»</span></code>) shifts an entire table, Nudge Cells shifts its major cells, or rows.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgJ2EnICsgM+KAv+KImCDipYog4oaVMjQgICMgQSBjaGFyYWN0ZXIgYXJyYXkKCuKfqCAgYSAgICAgICwgICAgIMK7YSAgICAgLCAgICDCu8uYYSDin6k=">↗️</a><pre> <span class='Value'>a</span> <span class='Gets'>←</span> <span class='String'>'a'</span> <span class='Function'>+</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Modifier2'>∘</span> <span class='Function'>⥊</span> <span class='Function'>↕</span><span class='Number'>24</span> <span class='Comment'># A character array
+</span>
+ <span class='Bracket'>⟨</span> <span class='Value'>a</span> <span class='Separator'>,</span> <span class='Function'>»</span><span class='Value'>a</span> <span class='Separator'>,</span> <span class='Function'>»</span><span class='Modifier'>˘</span><span class='Value'>a</span> <span class='Bracket'>⟩</span>
+┌─
+· ┌─ ┌─ ┌─
+ ╵"abcdefgh ╵" ╵" abcdefg
+ ijklmnop abcdefgh ijklmno
+ qrstuvwx" ijklmnop" qrstuvw"
+ ┘ ┘ ┘
+ ┘
+</pre>
+<p>What's it mean for Nudge to shift the &quot;entire table&quot;? The block above shows that is shifts downward, but what's really happening is that Nudge treats <code><span class='Value'>𝕩</span></code> as a collection of major cells—its rows—and shifts these. So it adds an entire row and moves the rest of the rows downwards. Nudge Cells appears similar, but it's acting independently on each row, and the values that it moves around are major cells of the row, that is, rank-0 units.</p>
+<p>Here's an example showing how Cells can be used to shift each row independently, even though it's not possible to shift columns like this (in fact the best way to would be to <a href="transpose.html">transpose</a> in order to work on rows). It uses the not-yet-introduced dyadic form of Cells, so you might want to come back to it after reading the next section.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKGkSLiiJjiiJgiKSDiipHiirjCu8uYIGE=">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>↑</span><span class='String'>&quot;∘∘&quot;</span><span class='Paren'>)</span> <span class='Function'>⊑</span><span class='Modifier2'>⊸</span><span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span>
+┌─
+╵"abcdefgh
+ ∘ijklmno
+ ∘∘qrstuv"
+ ┘
+</pre>
+<p>You can also see how Cells splits its argument into rows using a less array-oriented primitive: <a href="enclose.html">Enclose</a> just wraps each row up so that it appears as a separate element in the final result.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=PMuYIGE=">↗️</a><pre> <span class='Function'>&lt;</span><span class='Modifier'>˘</span> <span class='Value'>a</span>
+⟨ "abcdefgh" "ijklmnop" "qrstuvwx" ⟩
+</pre>
+<p>Enclose also comes in handy for the following task: join the rows in an array of lists, resulting in an array where each element is a joined row. The obvious guess would be &quot;join cells&quot;, <code><span class='Function'>∾</span><span class='Modifier'>˘</span></code>, but it doesn't work, because each <code><span class='Function'>∾</span></code> can return a result with a different length. Cells tries to make each result of <code><span class='Function'>∾</span></code> into a <em>cell</em>, when the problem was to use it as an <em>element</em>. But a 0-cell is an enclosed element, so we can close the gap by applying <code><span class='Function'>&lt;</span></code> to a joined list: <code><span class='Function'>&lt;</span><span class='Modifier2'>∘</span><span class='Function'>∾</span></code>.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHMg4oaQICJ3b3JkcyLigL8iZ28i4oC/ImhlcmUiIOKJjSAic29tZSLigL8ib3RoZXIi4oC/IndvcmRzIgoK4oi+y5ggcwoKPOKImOKIvsuYIHM=">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>s</span> <span class='Gets'>←</span> <span class='String'>&quot;words&quot;</span><span class='Ligature'>‿</span><span class='String'>&quot;go&quot;</span><span class='Ligature'>‿</span><span class='String'>&quot;here&quot;</span> <span class='Function'>≍</span> <span class='String'>&quot;some&quot;</span><span class='Ligature'>‿</span><span class='String'>&quot;other&quot;</span><span class='Ligature'>‿</span><span class='String'>&quot;words&quot;</span>
+┌─
+╵ "words" "go" "here"
+ "some" "other" "words"
+ ┘
+
+ <span class='Function'>∾</span><span class='Modifier'>˘</span> <span class='Value'>s</span>
+<span class='Error'>Error: >: Elements didn't have equal shapes (contained ⟨11⟩ and ⟨14⟩)</span>
+
+ <span class='Function'>&lt;</span><span class='Modifier2'>∘</span><span class='Function'>∾</span><span class='Modifier'>˘</span> <span class='Value'>s</span>
+⟨ "wordsgohere" "someotherwords" ⟩
+</pre>
+<p>This approach can apply to more complicated functions as well. And because the result of <code><span class='Function'>&lt;</span></code> always has the same shape, <code><span class='Bracket'>⟨⟩</span></code>, the function <code><span class='Function'>&lt;</span><span class='Modifier2'>∘</span><span class='Function'>𝔽</span><span class='Modifier'>˘</span></code> can never have a shape agreement error. So if <code><span class='Function'>𝔽</span><span class='Modifier'>˘</span></code> fails, it can't hurt to check <code><span class='Function'>&lt;</span><span class='Modifier2'>∘</span><span class='Function'>𝔽</span><span class='Modifier'>˘</span></code> and see what results <code><span class='Function'>𝔽</span></code> is returning.</p>
+<h3 id="two-arguments"><a class="header" href="#two-arguments">Two arguments</a></h3>
+<p>When given two arguments, Cells tries to pair their cells together. Starting simple, a unit array on either side will be paired with every cell of the other argument (and an atom is converted to an array).</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J+KImCcgwrvLmCBh">↗️</a><pre> <span class='String'>'∘'</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span>
+┌─
+╵"∘abcdefg
+ ∘ijklmno
+ ∘qrstuvw"
+ ┘
+</pre>
+<p>If you <em>want</em> to use this one-to-many behavior with an array, it'll take more work: since you're really only mapping over one argument, <a href="hook.html">bind</a> the other inside Cells.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IuKImOKImCIgwrvLmCBhCgoi4oiY4oiYIuKKuMK7y5ggYQ==">↗️</a><pre> <span class='String'>&quot;∘∘&quot;</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span>
+<span class='Error'>Error: ˘: Leading axis of arguments not equal (⟨2⟩ ≡ ≢𝕨, 3‿8 ≡ ≢𝕩)</span>
+
+ <span class='String'>&quot;∘∘&quot;</span><span class='Modifier2'>⊸</span><span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span>
+┌─
+╵"∘∘abcdef
+ ∘∘ijklmn
+ ∘∘qrstuv"
+ ┘
+</pre>
+<p>This is because the general case of Cells does one-to-one matching, pairing the first axis of one argument with the other. For this to work, the two arguments need to have the same length.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oICIwMTIiIMK7y5ggYSwgICgz4oC/4oiY4qWKIlVWV1hZWiIpIMK7y5ggYSDin6k=">↗️</a><pre> <span class='Bracket'>⟨</span> <span class='String'>&quot;012&quot;</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span><span class='Separator'>,</span> <span class='Paren'>(</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Modifier2'>∘</span><span class='Function'>⥊</span><span class='String'>&quot;UVWXYZ&quot;</span><span class='Paren'>)</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Bracket'>⟩</span>
+┌─
+· ┌─ ┌─
+ ╵"0abcdefg ╵"UVabcdef
+ 1ijklmno WXijklmn
+ 2qrstuvw" YZqrstuv"
+ ┘ ┘
+ ┘
+</pre>
+<p>The arguments might have different ranks: for example, <code><span class='String'>&quot;012&quot;</span></code> has rank 1 and <code><span class='Value'>a</span></code> has rank 2 above. That's fine: it just means Cells will pass arguments of rank 0 and 1 to its operand. You can see these arguments using <a href="pair.html">Pair</a> Cells, <code><span class='Function'>⋈</span><span class='Modifier'>˘</span></code>, so that each cell of the result is just a list of the two arguments used for that call.</p>
+<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IjAxMiIg4ouIy5ggYQ==">↗️</a><pre> <span class='String'>&quot;012&quot;</span> <span class='Function'>⋈</span><span class='Modifier'>˘</span> <span class='Value'>a</span>
+┌─
+╵ ┌· "abcdefgh"
+ ·'0'
+ ┘
+ ┌· "ijklmnop"
+ ·'1'
+ ┘
+ ┌· "qrstuvwx"
+ ·'2'
+ ┘
+ ┘
+</pre>