1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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> always becomes 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> returns <code><span class='Value'>π¨</span></code> if it's given and <code><span class='Value'>π©</span></code> if not.</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 needed 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>Moving on, 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 <a href="map.html#table">Table</a>, <code><span class='Modifier'>βΛ</span></code>, so that the operand function is called on each combination of elements in <code><span class='Value'>π©</span></code> 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 <a href="hook.html">Before</a> (<code><span class='Modifier2'>βΈ</span></code>) <a href="hook.html">and After</a> (<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>
|