diff options
Diffstat (limited to 'docs/doc')
| -rw-r--r-- | docs/doc/replicate.html | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/docs/doc/replicate.html b/docs/doc/replicate.html index 4387373b..f3fce857 100644 --- a/docs/doc/replicate.html +++ b/docs/doc/replicate.html @@ -231,3 +231,27 @@ ⟨ 1 1 3 0 1 ⟩ </pre> <p>The last of these is an extension defined in the language specification. As we said, the result of Indices is always sorted, so properly there's no argument that could return <code><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span></code>. But the index-counting function is very useful, so <code><span class='Function'>/</span><span class='Modifier'>⁼</span></code> is defined to implicitly sort its argument (which is still required to be a list of natural numbers). Since <code><span class='Function'>/</span><span class='Modifier'>⁼</span></code> is implemented as a single operation, it's the best way to perform this counting task.</p> +<h3 id="just-rank-1"><a class="header" href="#just-rank-1">Just rank 1?</a></h3> +<p>So if <code><span class='Value'>𝕩</span></code> is a boolean list, <code><span class='Function'>/</span><span class='Value'>𝕩</span></code> tells you where each 1 is located. What about a boolean array?</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHIg4oaQIDHCqOKMvigz4oC/OOKKj+KliikgM+KAvzbipYowCi9y">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>r</span> <span class='Gets'>←</span> <span class='Number'>1</span><span class='Modifier'>¨</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>8</span><span class='Function'>⊏⥊</span><span class='Paren'>)</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>6</span><span class='Function'>⥊</span><span class='Number'>0</span> +┌─ +╵ 0 0 0 1 0 0 + 0 0 1 0 0 0 + 0 0 0 0 0 0 + ┘ + <span class='Function'>/</span><span class='Value'>r</span> +<span class='Error'>Error: /: Argument must have rank 1 (3‿6 ≡ ≢𝕩)</span> +</pre> +<p>Error. But the two 1s are located at <code><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>3</span></code> and <code><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span></code>. What's wrong with those? First, let's note that you can find these indices if you need to, using Replicate. Make the list of all indices <code><span class='Function'>↕≢</span><span class='Value'>𝕩</span></code>, and filter <a href="compose.html#over">Over</a> <a href="reshape.html#deshape">Deshape</a> (<code><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=L+KXi+KliuKfnCjihpXiiaIpIHI=">↗️</a><pre> <span class='Function'>/</span><span class='Modifier2'>○</span><span class='Function'>⥊</span><span class='Modifier2'>⟜</span><span class='Paren'>(</span><span class='Function'>↕≢</span><span class='Paren'>)</span> <span class='Value'>r</span> +⟨ ⟨ 0 3 ⟩ ⟨ 1 2 ⟩ ⟩ +</pre> +<p>The issue with this function is that it's not consistent with the result of <code><span class='Function'>/</span></code> on a list. This is because the extension gives <a href="indices.html#element-indices">element indices</a>, which are lists, while the original <code><span class='Function'>/</span></code> gives single-number indices, which is only possible when <code><span class='Value'>𝕩</span></code> has rank 1.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=L+KXi+KliuKfnCjihpXiiaIpIDDigL8x4oC/MOKAvzHigL8w4oC/MOKAvzDigL8w4oC/MeKAvzAKCi8gICAgICAgIDDigL8x4oC/MOKAvzHigL8w4oC/MOKAvzDigL8w4oC/MeKAvzA=">↗️</a><pre> <span class='Function'>/</span><span class='Modifier2'>○</span><span class='Function'>⥊</span><span class='Modifier2'>⟜</span><span class='Paren'>(</span><span class='Function'>↕≢</span><span class='Paren'>)</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> +⟨ ⟨ 1 ⟩ ⟨ 3 ⟩ ⟨ 8 ⟩ ⟩ + + <span class='Function'>/</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> +⟨ 1 3 8 ⟩ +</pre> +<p>So these functions can't be the same primitive. The only thing we can do—what some APLs do—is to use index lists when <code><span class='Value'>𝕩</span></code> has rank not equal to 1 but index numbers otherwise. But supporting a partial function is hazardous: it means that code that works for ranks 4, 3, 2… might not work for rank 1. This isn't how BQN does things. So if you need this functionality, you need to spell it out.</p> +<p>Which isn't too simple? The other part of the story, and why I think <code><span class='Function'>/</span><span class='Modifier2'>○</span><span class='Function'>⥊</span><span class='Modifier2'>⟜</span><span class='Paren'>(</span><span class='Function'>↕≢</span><span class='Paren'>)</span></code> is good enough despite its length, is that I just don't like element indices. They force array nesting here, which is complicated and slow. There's usually a better approach, such as using arithmetic to skip indices entirely, or <code><span class='Modifier2'>⌾</span><span class='Function'>⥊</span></code> (<a href="under.html">Under</a> Deshape) to temporarily work on the array as a list.</p> |
