aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/swap.html
diff options
context:
space:
mode:
authorMarshall Lochbaum <mwlochbaum@gmail.com>2022-04-13 16:05:00 -0400
committerMarshall Lochbaum <mwlochbaum@gmail.com>2022-04-13 20:56:52 -0400
commitd291cb7faf2c1856e2cea486b0d935bbb16db2c9 (patch)
treecaaed54bea89d302d68dcb368ef919466f9d07d0 /docs/doc/swap.html
parent44c184a54ce4cb101b70edca9aef271462ee5d66 (diff)
Documentation for Self/Swap
Diffstat (limited to 'docs/doc/swap.html')
-rw-r--r--docs/doc/swap.html79
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'>&quot; +&quot;</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>