diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-04-15 19:23:10 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-04-15 19:26:07 -0400 |
| commit | 8ad841e92f70fba73a5c778aa972186387f9ff5c (patch) | |
| tree | 44d051e0c4d984ddc630cdbdb456d3b2b9dbf7cf /docs/doc | |
| parent | 08115ba7569cbd21cfe89be152a311e01ebb6c88 (diff) | |
Documentation for Before and After
Diffstat (limited to 'docs/doc')
| -rw-r--r-- | docs/doc/hook.html | 161 | ||||
| -rw-r--r-- | docs/doc/index.html | 1 | ||||
| -rw-r--r-- | docs/doc/primitive.html | 4 |
3 files changed, 164 insertions, 2 deletions
diff --git a/docs/doc/hook.html b/docs/doc/hook.html new file mode 100644 index 00000000..22054800 --- /dev/null +++ b/docs/doc/hook.html @@ -0,0 +1,161 @@ +<head> + <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/> + <link href="../style.css" rel="stylesheet"/> + <title>BQN: Before and After</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="before-and-after"><a class="header" href="#before-and-after">Before and After</a></h1> +<p>(<a href="https://aplwiki.com/wiki/File:Before_and_after.jpg">This joke</a> has already been claimed by APL, unfortunately)</p> +<p><em>Also see <a href="../tutorial/combinator.html#before-and-after">this tutorial section</a> for an introduction that doesn't require so much context to understand.</em></p> +<svg viewBox='-51 0 672 270'> + <g font-size='20px' text-anchor='middle' transform='translate(145,20)'> + <rect class='code' stroke-width='1' rx='12' x='-120.4' y='1' width='240.8' height='205'/> + <text dy='0.32em' y='223' fill='currentColor'>Before</text> + <g font-size='21px' font-family='BQN,monospace' transform='translate(-60.87,25)'> + <text dy='0.32em' y='155' font-size='19px'><tspan class='Function'>π½</tspan><tspan class='Modifier2'>βΈ</tspan><tspan class='Function'>πΎ</tspan> <tspan class='Value'>π©</tspan></text> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0L-32 57'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M-32 57L0 114'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0Q41.6 57 0 114'/> + <circle r='12' class='code' stroke-width='0' cx='0' cy='0'/> + <circle r='12' class='code' stroke-width='0' cx='-32' cy='57'/> + <circle r='12' class='code' stroke-width='0' cx='0' cy='114'/> + <text dy='0.32em' x='0' y='0'><tspan class='Function'>πΎ</tspan></text> + <text dy='0.32em' x='-32' y='57'><tspan class='Function'>π½</tspan></text> + <text dy='0.32em' x='0' y='114'><tspan class='Value'>π©</tspan></text> + </g> + <g font-size='21px' font-family='BQN,monospace' transform='translate(60.87,25)'> + <text dy='0.32em' y='155' font-size='19px'><tspan class='Value'>π¨</tspan> <tspan class='Function'>π½</tspan><tspan class='Modifier2'>βΈ</tspan><tspan class='Function'>πΎ</tspan> <tspan class='Value'>π©</tspan></text> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0L-32 57'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M-32 57L-32 114'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0C40 57 32 51.3 32 114'/> + <circle r='12' class='code' stroke-width='0' cx='0' cy='0'/> + <circle r='12' class='code' stroke-width='0' cx='-32' cy='57'/> + <circle r='12' class='code' stroke-width='0' cx='-32' cy='114'/> + <circle r='12' class='code' stroke-width='0' cx='32' cy='114'/> + <text dy='0.32em' x='0' y='0'><tspan class='Function'>πΎ</tspan></text> + <text dy='0.32em' x='-32' y='57'><tspan class='Function'>π½</tspan></text> + <text dy='0.32em' x='-32' y='114'><tspan class='Value'>π¨</tspan></text> + <text dy='0.32em' x='32' y='114'><tspan class='Value'>π©</tspan></text> + </g> + </g> + <g font-size='20px' text-anchor='middle' transform='translate(425,20)'> + <rect class='code' stroke-width='1' rx='12' x='-120.4' y='1' width='240.8' height='205'/> + <text dy='0.32em' y='223' fill='currentColor'>After</text> + <g font-size='21px' font-family='BQN,monospace' transform='translate(-60.87,25)'> + <text dy='0.32em' y='155' font-size='19px'><tspan class='Function'>π½</tspan><tspan class='Modifier2'>β</tspan><tspan class='Function'>πΎ</tspan> <tspan class='Value'>π©</tspan></text> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0Q-41.6 57 0 114'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0L32 57'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M32 57L0 114'/> + <circle r='12' class='code' stroke-width='0' cx='0' cy='0'/> + <circle r='12' class='code' stroke-width='0' cx='32' cy='57'/> + <circle r='12' class='code' stroke-width='0' cx='0' cy='114'/> + <text dy='0.32em' x='0' y='0'><tspan class='Function'>π½</tspan></text> + <text dy='0.32em' x='32' y='57'><tspan class='Function'>πΎ</tspan></text> + <text dy='0.32em' x='0' y='114'><tspan class='Value'>π©</tspan></text> + </g> + <g font-size='21px' font-family='BQN,monospace' transform='translate(60.87,25)'> + <text dy='0.32em' y='155' font-size='19px'><tspan class='Value'>π¨</tspan> <tspan class='Function'>π½</tspan><tspan class='Modifier2'>β</tspan><tspan class='Function'>πΎ</tspan> <tspan class='Value'>π©</tspan></text> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0C-40 57 -32 51.3 -32 114'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0L32 57'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M32 57L32 114'/> + <circle r='12' class='code' stroke-width='0' cx='0' cy='0'/> + <circle r='12' class='code' stroke-width='0' cx='32' cy='57'/> + <circle r='12' class='code' stroke-width='0' cx='-32' cy='114'/> + <circle r='12' class='code' stroke-width='0' cx='32' cy='114'/> + <text dy='0.32em' x='0' y='0'><tspan class='Function'>π½</tspan></text> + <text dy='0.32em' x='32' y='57'><tspan class='Function'>πΎ</tspan></text> + <text dy='0.32em' x='-32' y='114'><tspan class='Value'>π¨</tspan></text> + <text dy='0.32em' x='32' y='114'><tspan class='Value'>π©</tspan></text> + </g> + </g> +</svg> + +<p>The "hook" combinators Before and After serve a few purposes in BQN. The important thing to remember: the pointy side goes towards the first function to be executed, and the next function that returns the final result is at the ring side. If the pointy-side function is actually a constant like a number, then the ring-side function just gets applied to that constant and one of the arguments. This is the thing Haskell programmers are constantly telling each other isn't called currying, or "Bind" in BQN.</p> +<table> +<thead> +<tr> +<th>Name</th> +<th><code><span class='Function'>Cmp</span></code></th> +<th><code><span class='Function'>Cmp</span> <span class='Value'>π©</span></code></th> +<th><code><span class='Value'>π¨</span> <span class='Function'>Cmp</span> <span class='Value'>π©</span></code></th> +<th>Unified</th> +<th>Train</th> +</tr> +</thead> +<tbody> +<tr> +<td>Before</td> +<td><code><span class='Function'>F</span><span class='Modifier2'>βΈ</span><span class='Function'>G</span></code></td> +<td><code><span class='Paren'>(</span><span class='Function'>F</span><span class='Value'>π©</span><span class='Paren'>)</span> <span class='Function'>G</span> <span class='Value'>π©</span></code></td> +<td><code><span class='Paren'>(</span><span class='Function'>F</span><span class='Value'>π¨</span><span class='Paren'>)</span> <span class='Function'>G</span> <span class='Value'>π©</span></code></td> +<td><code><span class='Brace'>{</span><span class='Paren'>(</span><span class='Function'>π½</span><span class='Value'>π¨</span><span class='Function'>β£</span><span class='Value'>π©</span><span class='Paren'>)</span><span class='Function'>πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span></code></td> +<td><code><span class='Function'>F</span><span class='Modifier2'>β</span><span class='Function'>β£</span> <span class='Function'>G</span> <span class='Function'>β’</span></code></td> +</tr> +<tr> +<td>After</td> +<td><code><span class='Function'>F</span><span class='Modifier2'>β</span><span class='Function'>G</span></code></td> +<td><code><span class='Value'>π©</span> <span class='Function'>F</span> <span class='Paren'>(</span><span class='Function'>G</span><span class='Value'>π©</span><span class='Paren'>)</span></code></td> +<td><code><span class='Value'>π¨</span> <span class='Function'>F</span> <span class='Paren'>(</span><span class='Function'>G</span><span class='Value'>π©</span><span class='Paren'>)</span></code></td> +<td><code><span class='Brace'>{</span><span class='Paren'>(</span><span class='Value'>π¨</span><span class='Function'>β£</span><span class='Value'>π©</span><span class='Paren'>)</span><span class='Function'>π½πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span></code></td> +<td><code><span class='Function'>β£</span> <span class='Function'>F</span> <span class='Function'>G</span><span class='Modifier2'>β</span><span class='Function'>β’</span></code></td> +</tr> +</tbody> +</table> +<h2 id="description"><a class="header" href="#description">Description</a></h2> +<p>In the general case, I think of Before as using <code><span class='Function'>π½</span></code> as a preprocessing function applied to <code><span class='Value'>π¨</span></code> (when there are two arguments) and After as using <code><span class='Function'>πΎ</span></code> as preprocessing for <code><span class='Value'>π©</span></code>. Then the other operand is called on the result and remaining argument. Here are some simple calls with Pair (<code><span class='Function'>β</span></code>): the result is a pair that corresponds to <code><span class='Value'>π¨</span><span class='Ligature'>βΏ</span><span class='Value'>π©</span></code>, but one or the other result has been modified by the pointy-side function.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=OSDiiJriirjii4ggMgoKOSDii4jin5zihpUgMg==">βοΈ</a><pre> <span class='Number'>9</span> <span class='Function'>β</span><span class='Modifier2'>βΈ</span><span class='Function'>β</span> <span class='Number'>2</span> +β¨ 3 2 β© + + <span class='Number'>9</span> <span class='Function'>β</span><span class='Modifier2'>β</span><span class='Function'>β</span> <span class='Number'>2</span> +β¨ 9 β¨ 0 1 β© β© +</pre> +<p>When only one argument is given, it's used in both positions, so that the arguments to the final function are <code><span class='Value'>π©</span></code> and a function applied to <code><span class='Value'>π©</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4ouI4p+c4oaVIDU=">βοΈ</a><pre> <span class='Function'>β</span><span class='Modifier2'>β</span><span class='Function'>β</span> <span class='Number'>5</span> +β¨ 5 β¨ 0 1 2 3 4 β© β© +</pre> +<p>This can be used to make a "filter" pattern using <a href="replicate.html">Replicate</a> (<code><span class='Function'>/</span></code>). The difference is that Replicate takes a list <code><span class='Value'>π©</span></code> and boolean list <code><span class='Value'>π¨</span></code> indicating which elements to keep, but filter should take a list and a function that says whether to keep each element. The pattern is <code><span class='Function'>F</span><span class='Modifier'>Β¨</span><span class='Modifier2'>βΈ</span><span class='Function'>/</span> <span class='Value'>x</span></code>, expanding to <code><span class='Paren'>(</span><span class='Function'>F</span><span class='Modifier'>Β¨</span><span class='Value'>x</span><span class='Paren'>)</span> <span class='Function'>/</span> <span class='Value'>x</span></code>. Here's a list filtered with the function <code><span class='Brace'>{</span><span class='Value'>π©</span><span class='Function'><</span><span class='Number'>0</span><span class='Brace'>}</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=e/Cdlak8MH3CqOKKuC8gNOKAv8KvMuKAvzHigL/CrzPigL/CrzM=">βοΈ</a><pre> <span class='Brace'>{</span><span class='Value'>π©</span><span class='Function'><</span><span class='Number'>0</span><span class='Brace'>}</span><span class='Modifier'>Β¨</span><span class='Modifier2'>βΈ</span><span class='Function'>/</span> <span class='Number'>4</span><span class='Ligature'>βΏ</span><span class='Number'>Β―2</span><span class='Ligature'>βΏ</span><span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Number'>Β―3</span><span class='Ligature'>βΏ</span><span class='Number'>Β―3</span> +β¨ Β―2 Β―3 Β―3 β© +</pre> +<p>As <code><span class='Function'><</span></code> is a pervasive function, there's no need for the Each (<code><span class='Modifier'>Β¨</span></code>) in this case, and the clunky block function <code><span class='Brace'>{</span><span class='Value'>π©</span><span class='Function'><</span><span class='Number'>0</span><span class='Brace'>}</span></code> can also be written smaller with a combinator, as <code><span class='Function'><</span><span class='Modifier2'>β</span><span class='Number'>0</span></code>. More on that in the next sectionβ¦</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=POKfnDDiirgvIDTigL/CrzLigL8x4oC/wq8z4oC/wq8z">βοΈ</a><pre> <span class='Function'><</span><span class='Modifier2'>β</span><span class='Number'>0</span><span class='Modifier2'>βΈ</span><span class='Function'>/</span> <span class='Number'>4</span><span class='Ligature'>βΏ</span><span class='Number'>Β―2</span><span class='Ligature'>βΏ</span><span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Number'>Β―3</span><span class='Ligature'>βΏ</span><span class='Number'>Β―3</span> +β¨ Β―2 Β―3 Β―3 β© +</pre> +<h2 id="bind"><a class="header" href="#bind">Bind</a></h2> +<p>"Bind" isn't a special case of Before and After, but instead a description of one way to use them. Let's take a look at the example from the previous section:</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=POKfnDAgIDTigL/CrzLigL8x4oC/wq8z4oC/wq8z">βοΈ</a><pre> <span class='Function'><</span><span class='Modifier2'>β</span><span class='Number'>0</span> <span class='Number'>4</span><span class='Ligature'>βΏ</span><span class='Number'>Β―2</span><span class='Ligature'>βΏ</span><span class='Number'>1</span><span class='Ligature'>βΏ</span><span class='Number'>Β―3</span><span class='Ligature'>βΏ</span><span class='Number'>Β―3</span> +β¨ 0 1 0 1 1 β© +</pre> +<p>If we expand <code><span class='Function'><</span><span class='Modifier2'>β</span><span class='Number'>0</span> <span class='Value'>x</span></code>, we get <code><span class='Value'>x</span> <span class='Function'><</span> <span class='Paren'>(</span><span class='Number'>0</span> <span class='Value'>x</span><span class='Paren'>)</span></code>, which doesn't quite make sense. That's because <code><span class='Number'>0</span></code> has a subject role, but <code><span class='Modifier2'>β</span></code> always applies its operands as functions. It's more accurate to use <code><span class='Value'>x</span> <span class='Function'><</span> <span class='Paren'>(</span><span class='Number'>0</span><span class='Brace'>{</span><span class='Function'>π½</span><span class='Brace'>}</span> <span class='Value'>x</span><span class='Paren'>)</span></code>, or just skip ahead to <code><span class='Value'>x</span> <span class='Function'><</span> <span class='Number'>0</span></code>.</p> +<p>Similar reasoning gives the following expansions:</p> +<table> +<thead> +<tr> +<th><code><span class='Function'>Cmp</span></code></th> +<th><code><span class='Number'>0</span><span class='Modifier2'>βΈ</span><span class='Function'><</span></code></th> +<th><code><span class='Function'><</span><span class='Modifier2'>β</span><span class='Number'>0</span></code></th> +</tr> +</thead> +<tbody> +<tr> +<td><code> <span class='Function'>Cmp</span> <span class='Value'>x</span></code></td> +<td><code><span class='Number'>0</span> <span class='Function'><</span> <span class='Value'>x</span></code></td> +<td><code><span class='Value'>x</span> <span class='Function'><</span> <span class='Number'>0</span></code></td> +</tr> +<tr> +<td><code><span class='Value'>w</span> <span class='Function'>Cmp</span> <span class='Value'>x</span></code></td> +<td><code><span class='Number'>0</span> <span class='Function'><</span> <span class='Value'>x</span></code></td> +<td><code><span class='Value'>w</span> <span class='Function'><</span> <span class='Number'>0</span></code></td> +</tr> +</tbody> +</table> +<p>Note that when there are two arguments, the constant "swallows" the one on the same side, so that the function is applied to the constant and the argument on the <em>opposite</em> side.</p> +<p>As in a train, if you want to use a function as a constant then you need to be explicity about it, with the <a href="constant.html">Constant</a> (<code><span class='Modifier'>Λ</span></code>) modifier.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyDii4jin5wo4oyKy5kp4oq44qWKICdhJyvihpUxMg==">βοΈ</a><pre> <span class='Number'>3</span> <span class='Function'>β</span><span class='Modifier2'>β</span><span class='Paren'>(</span><span class='Function'>β</span><span class='Modifier'>Λ</span><span class='Paren'>)</span><span class='Modifier2'>βΈ</span><span class='Function'>β₯</span> <span class='String'>'a'</span><span class='Function'>+β</span><span class='Number'>12</span> +ββ +β΅"abcd + efgh + ijkl" + β +</pre> +<p>In the more extreme case of wanting a <em>modifier</em> operand, you might try <code><span class='Function'>β</span><span class='Modifier2'>β</span><span class='Paren'>(</span><span class='Brace'>{</span><span class='Modifier2'>β</span><span class='Brace'>}</span><span class='Modifier'>Λ</span><span class='Paren'>)</span><span class='Modifier2'>βΈ</span><span class='Function'>β₯</span></code>, or <code><span class='Paren'>(</span><span class='Function'>β£β</span><span class='Brace'>{</span><span class='Modifier2'>β</span><span class='Brace'>}</span><span class='Modifier'>Λ</span><span class='Paren'>)</span><span class='Modifier2'>βΈ</span><span class='Function'>β₯</span></code>, or just cheat with <code><span class='Function'>βΎ</span><span class='Modifier2'>β</span><span class='Bracket'>β¨</span><span class='Modifier2'>β</span><span class='Bracket'>β©</span><span class='Modifier2'>βΈ</span><span class='Function'>β₯</span></code>.</p> diff --git a/docs/doc/index.html b/docs/doc/index.html index 66bdbfec..82af1d4c 100644 --- a/docs/doc/index.html +++ b/docs/doc/index.html @@ -48,6 +48,7 @@ <li><a href="shape.html">Array dimensions</a> (<code><span class='Function'>β’=β </span></code>)</li> <li><a href="assert.html">Assert and Catch</a> (<code><span class='Function'>!</span></code> and <code><span class='Modifier2'>β</span></code>)</li> <li><a href="compose.html">Atop and Over</a> (<code><span class='Modifier2'>ββ</span></code>)</li> +<li><a href="hook.html">Before and After</a> (<code><span class='Modifier2'>βΈβ</span></code>)</li> <li><a href="choose.html">Choose</a> (<code><span class='Modifier2'>βΆ</span></code>)</li> <li><a href="constant.html">Constant</a> (<code><span class='Modifier'>Λ</span></code>)</li> <li><a href="reshape.html">Deshape and Reshape</a> (<code><span class='Function'>β₯</span></code>)</li> diff --git a/docs/doc/primitive.html b/docs/doc/primitive.html index aa3fd087..fd153f5a 100644 --- a/docs/doc/primitive.html +++ b/docs/doc/primitive.html @@ -449,13 +449,13 @@ </tr> <tr> <td><code><span class='Modifier2'>βΈ</span></code></td> -<td>Before/Bind</td> +<td><a href="hook.html">Before/Bind</a></td> <td><code><span class='Brace'>{</span><span class='Paren'>(</span><span class='Function'>π½</span><span class='Value'>π¨</span><span class='Function'>β£</span><span class='Value'>π©</span><span class='Paren'>)</span><span class='Function'>πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span></code></td> <td><code><span class='Function'>πΎ</span></code>'s left argument comes from <code><span class='Function'>π½</span></code></td> </tr> <tr> <td><code><span class='Modifier2'>β</span></code></td> -<td>After/Bind</td> +<td><a href="hook.html">After/Bind</a></td> <td><code><span class='Brace'>{</span><span class='Paren'>(</span><span class='Value'>π¨</span><span class='Function'>β£</span><span class='Value'>π©</span><span class='Paren'>)</span><span class='Function'>π½πΎ</span><span class='Value'>π©</span><span class='Brace'>}</span></code></td> <td><code><span class='Function'>π½</span></code>'s right argument comes from <code><span class='Function'>πΎ</span></code></td> </tr> |
