diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-04-13 16:05:00 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-04-13 20:56:52 -0400 |
| commit | d291cb7faf2c1856e2cea486b0d935bbb16db2c9 (patch) | |
| tree | caaed54bea89d302d68dcb368ef919466f9d07d0 /docs/doc/swap.html | |
| parent | 44c184a54ce4cb101b70edca9aef271462ee5d66 (diff) | |
Documentation for Self/Swap
Diffstat (limited to 'docs/doc/swap.html')
| -rw-r--r-- | docs/doc/swap.html | 79 |
1 files changed, 79 insertions, 0 deletions
diff --git a/docs/doc/swap.html b/docs/doc/swap.html new file mode 100644 index 00000000..190b0d0a --- /dev/null +++ b/docs/doc/swap.html @@ -0,0 +1,79 @@ +<head> + <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/> + <link href="../style.css" rel="stylesheet"/> + <title>BQN: Self and Swap</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="self-and-swap"><a class="header" href="#self-and-swap">Self and Swap</a></h1> +<svg viewBox='-191 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'>Self/Swap</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='Modifier'>Λ</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 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='0' cy='114'/> + <text dy='0.32em' x='0' y='0'><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='Modifier'>Λ</tspan> <tspan class='Value'>π©</tspan></text> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0C-40 28.5 0 57 32 114'/> + <path class='yellow' style='fill:none' stroke-width='2' d='M0 0C40 28.5 0 57 -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='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='114'><tspan class='Value'>π¨</tspan></text> + <text dy='0.32em' x='32' y='114'><tspan class='Value'>π©</tspan></text> + </g> + </g> +</svg> + +<p>Have the arguments to a function, but not in the right places? Self/Swap (<code><span class='Modifier'>Λ</span></code>) will fix it for you. There are only two APL-style 1-modifiersβthat is, operands used as functions and applied to argumentsβthat make sense, and <code><span class='Modifier'>Λ</span></code> is both of them. It always calls its operand with two arguments: if there are two arguments to begin with, they are exchanged (Swap), and if there's only one, it's used for both arguments (Self).</p> +<table> +<thead> +<tr> +<th>Name</th> +<th>Call</th> +<th align="center">Definition</th> +</tr> +</thead> +<tbody> +<tr> +<td>Self</td> +<td><code> <span class='Function'>F</span><span class='Modifier'>Λ</span><span class='Value'>π©</span></code></td> +<td align="center"><code><span class='Value'>π©</span><span class='Function'>F</span><span class='Value'>π©</span></code></td> +</tr> +<tr> +<td>Swap</td> +<td><code><span class='Value'>π¨</span><span class='Function'>F</span><span class='Modifier'>Λ</span><span class='Value'>π©</span></code></td> +<td align="center"><code><span class='Value'>π©</span><span class='Function'>F</span><span class='Value'>π¨</span></code></td> +</tr> +</tbody> +</table> +<p>Since <code><span class='Value'>π©</span></code> is always the left argument, these two definitions can be unified as <code><span class='Brace'>{</span><span class='Value'>π©</span><span class='Function'>π½</span><span class='Value'>π¨</span><span class='Function'>β£</span><span class='Value'>π©</span><span class='Brace'>}</span></code>, noting that <a href="identity.html">Left</a> becomes a plain identity function when the left argument <code><span class='Value'>π¨</span></code> isn't given.</p> +<p>Swap is arguably less transformative. Some common examples are <code><span class='Function'>-</span><span class='Modifier'>Λ</span></code> and <code><span class='Function'>Γ·</span><span class='Modifier'>Λ</span></code>, since these two functions run the <a href="../commentary/problems.html#subtraction-division-and-span-are-backwards">wrong way</a> for BQN's evaluation order. This is very often useful in <a href="tacit.html">tacit</a> programming, and less useful for explicit code. While it sometimes allows for shorter code by making a pair of parentheses unnecessary (say, <code><span class='Paren'>(</span><span class='Value'>a</span><span class='Function'>Γ</span><span class='Value'>b</span><span class='Paren'>)</span><span class='Function'>-</span><span class='Value'>c</span></code> is <code><span class='Value'>c</span><span class='Function'>-</span><span class='Modifier'>Λ</span><span class='Value'>a</span><span class='Function'>Γ</span><span class='Value'>b</span></code>), I personally don't think this is always a good idea. My opinion is that it should be used when it makes the semantics a better fit for BQN, but putting the primary argument on the right and a secondary or control argument on the left.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J2EnIOKLiMucICdiJwoKIiArIiDiio/LnCAw4oC/MeKAvzHigL8w4oC/MOKJjTHigL8w4oC/MeKAvzDigL8x">βοΈ</a><pre> <span class='String'>'a'</span> <span class='Function'>β</span><span class='Modifier'>Λ</span> <span class='String'>'b'</span> +"ba" + + <span class='String'>" +"</span> <span class='Function'>β</span><span class='Modifier'>Λ</span> <span class='Number'>0</span><span class='Ligature'>βΏ</span><span class='Number'>1</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='Function'>β</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'>1</span> +ββ +β΅" ++ + + + +" + β +</pre> +<p>Self re-uses one argument twice. In this way it's a little like <a href="compose.html">Over</a>, which re-uses one <em>function</em> twice. A common combination is with Table, <code><span class='Modifier'>βΛ</span></code>, so that the operand function is called on each combination of elements from the argument to form a square result. For example, <code><span class='Function'>=</span><span class='Modifier'>βΛ</span></code> applied to <code><span class='Function'>β</span><span class='Value'>n</span></code> gives the identity matrix of size <code><span class='Value'>n</span></code>.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w5fLnCA0Cgo94oycy5wg4oaVMw==">βοΈ</a><pre> <span class='Function'>Γ</span><span class='Modifier'>Λ</span> <span class='Number'>4</span> +16 + + <span class='Function'>=</span><span class='Modifier'>βΛ</span> <span class='Function'>β</span><span class='Number'>3</span> +ββ +β΅ 1 0 0 + 0 1 0 + 0 0 1 + β +</pre> +<p>Note that Self isn't needed with Before (<code><span class='Modifier2'>βΈ</span></code>) and After (<code><span class='Modifier2'>β</span></code>), which essentially have a copy built in: for example <code><span class='Function'>F</span><span class='Modifier2'>βΈ</span><span class='Function'>G</span> <span class='Value'>π©</span></code> is the same as <code><span class='Function'>F</span><span class='Modifier2'>βΈ</span><span class='Function'>G</span><span class='Modifier'>Λ</span> <span class='Value'>π©</span></code> by definition.</p> |
