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
|
<head>
<link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
<link href="../style.css" rel="stylesheet"/>
<title>BQN: Identity functions</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="identity-functions"><a class="header" href="#identity-functions">Identity functions</a></h1>
<p>Here are the simplest functions in BQN: Right (<code><span class='Function'>⊢</span></code>) always returns its right argument, and Left (<code><span class='Function'>⊣</span></code>) returns its left argument if called with two arguments, and the right argument otherwise.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiICJvbmx5IgoK4oqjICJvbmx5IgoKImxlZnQiIOKKoiAicmlnaHQiCgoibGVmdCIg4oqjICJyaWdodCI=">↗️</a><pre> <span class='Function'>⊢</span> <span class='String'>"only"</span>
"only"
<span class='Function'>⊣</span> <span class='String'>"only"</span>
"only"
<span class='String'>"left"</span> <span class='Function'>⊢</span> <span class='String'>"right"</span>
"right"
<span class='String'>"left"</span> <span class='Function'>⊣</span> <span class='String'>"right"</span>
"left"
</pre>
<p>Depending on your past experiences, this could cause some confusion: built-in support for functions that do nothing? Documentation should say why a feature's there and how to use it, not just what it does, so we'll try to address this below. The most important single use is for <a href="tacit.html">tacit</a> programming, but there are a variety of other uses as well.</p>
<p>Of course, it's easy to write block functions <code><span class='Brace'>{</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code> and <code><span class='Brace'>{</span><span class='Value'>𝕨</span><span class='Brace'>}</span></code> that return particular arguments. While I would already make <code><span class='Function'>⊣</span></code> and <code><span class='Function'>⊢</span></code> primitives just because they are common and important, there are also specific disadvantages to using blocks. They fail to indicate that there are no side effects, as primitives would, and they also need special casing for the interpreter to manipulate them when applying <a href="undo.html">Undo</a> (<code><span class='Modifier'>⁼</span></code>) or making other inferences.</p>
<h2 id="filling-arrays"><a class="header" href="#filling-arrays">Filling arrays</a></h2>
<p>What's the easiest way to create a matrix with 0 on the first row, 1 on the second, and so on? Probably this one, with <a href="map.html#table">table</a>:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKGlTQpIOKKo+KMnCDihpU1">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>↕</span><span class='Number'>4</span><span class='Paren'>)</span> <span class='Function'>⊣</span><span class='Modifier'>⌜</span> <span class='Function'>↕</span><span class='Number'>5</span>
┌─
╵ 0 0 0 0 0
1 1 1 1 1
2 2 2 2 2
3 3 3 3 3
┘
</pre>
<p>The right argument <code><span class='Function'>↕</span><span class='Number'>5</span></code> could be any length-5 list, as its values aren't used. With <code><span class='Number'>5</span><span class='Function'>⥊</span><span class='Number'>0</span></code>, we could use <code><span class='Function'>+</span><span class='Modifier'>⌜</span></code> instead, but requiring a specific argument seems artificial. A similar pattern applies with <a href="map.html#each">Each</a>:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKMveKGlTQpIOKKo8KoIOKGlTTigL81">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>⌽↕</span><span class='Number'>4</span><span class='Paren'>)</span> <span class='Function'>⊣</span><span class='Modifier'>¨</span> <span class='Function'>↕</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>5</span>
┌─
╵ 3 3 3 3 3
2 2 2 2 2
1 1 1 1 1
0 0 0 0 0
┘
</pre>
<p>A more powerful pattern is with dyadic <a href="under.html">Under</a> (<code><span class='Modifier2'>⌾</span></code>): unselected parts of the result will use values from <code><span class='Value'>𝕩</span></code>. If <code><span class='Function'>𝔽</span></code> is <code><span class='Function'>⊣</span></code>, then the selected ones will use values from <code><span class='Value'>𝕨</span></code>, merging these arrays together.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IkFCQ0RFIiDiiqPijL4oMOKAvzHigL8x4oC/MOKAvzDiirgvKSAiYWJjZGUiCgrin6gid3h5IuKAvyJ6IixA4p+pIOKKo+KMvigx4oqR4oqR4oiY4oqRKSDin6jin6jihpUzLOKGlTLin6ksNOKAvzXin6k=">↗️</a><pre> <span class='String'>"ABCDE"</span> <span class='Function'>⊣</span><span class='Modifier2'>⌾</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'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Modifier2'>⊸</span><span class='Function'>/</span><span class='Paren'>)</span> <span class='String'>"abcde"</span>
"aBCde"
<span class='Bracket'>⟨</span><span class='String'>"wxy"</span><span class='Ligature'>‿</span><span class='String'>"z"</span><span class='Separator'>,</span><span class='String'>@</span><span class='Bracket'>⟩</span> <span class='Function'>⊣</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>1</span><span class='Function'>⊑⊑</span><span class='Modifier2'>∘</span><span class='Function'>⊑</span><span class='Paren'>)</span> <span class='Bracket'>⟨⟨</span><span class='Function'>↕</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Function'>↕</span><span class='Number'>2</span><span class='Bracket'>⟩</span><span class='Separator'>,</span><span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Bracket'>⟩</span>
┌─
· ⟨ ⟨ 0 'x' 2 ⟩ ⟨ 0 1 ⟩ ⟩ ⟨ 4 5 ⟩
┘
</pre>
<p>This method can replace even values nested deeply in arrays, as long as you can write the function to get at them. The parts that aren't accessed don't even need to have matching shapes!</p>
<h2 id="as-a-variable"><a class="header" href="#as-a-variable">As a variable</a></h2>
<p>Suppose you want a list of a matrix, its transpose, and its negation. One way to do this is to put together a list of functions for each of these values: the first one is an identity.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqi4oC/4o2J4oC/LSB78J2VjvCdlal9wqg8IDDigL/CrzHiiY0x4oC/MA==">↗️</a><pre> <span class='Function'>⊢</span><span class='Ligature'>‿</span><span class='Function'>⍉</span><span class='Ligature'>‿</span><span class='Function'>-</span> <span class='Brace'>{</span><span class='Function'>𝕎</span><span class='Value'>𝕩</span><span class='Brace'>}</span><span class='Modifier'>¨</span><span class='Function'><</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>¯1</span><span class='Function'>≍</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span>
┌─
· ┌─ ┌─ ┌─
╵ 0 ¯1 ╵ 0 1 ╵ 0 1
1 0 ¯1 0 ¯1 0
┘ ┘ ┘
┘
</pre>
<p>Here <code><span class='Function'>⊢</span></code> ends up being used as <code><span class='Function'>𝕎</span></code>. A similar case might be a function or program with a caller-specified processing step. For example, a function to write some kind of file, with a parameter function to encrypt data before writing. To use no encryption, you'd pass a parameter <code><span class='Function'>⊢</span></code>. Or it might happen that you write a Choose (<code><span class='Modifier2'>◶</span></code>) expression where one of the cases should do nothing <code><span class='Function'>⊢</span></code>, or return the left argument <code><span class='Function'>⊣</span></code>.</p>
<h2 id="in-tacit-functions"><a class="header" href="#in-tacit-functions">In tacit functions</a></h2>
<p>In a <a href="tacit.html">tacit</a> context, <code><span class='Function'>⊣</span></code> is roughly equivalent to <code><span class='Value'>𝕨</span></code> and <code><span class='Function'>⊢</span></code> to <code><span class='Value'>𝕩</span></code>. In some (not too common) cases, it's even possible to translate a block function to tacit code directly by replacing the variables in this way.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyB78J2VqS3wnZWow7cxK/Cdlal9IDUKMyAo4oqiLeKKo8O3MSviiqIpIDU=">↗️</a><pre> <span class='Number'>3</span> <span class='Brace'>{</span><span class='Value'>𝕩</span><span class='Function'>-</span><span class='Value'>𝕨</span><span class='Function'>÷</span><span class='Number'>1</span><span class='Function'>+</span><span class='Value'>𝕩</span><span class='Brace'>}</span> <span class='Number'>5</span>
4.5
<span class='Number'>3</span> <span class='Paren'>(</span><span class='Function'>⊢-⊣÷</span><span class='Number'>1</span><span class='Function'>+⊢</span><span class='Paren'>)</span> <span class='Number'>5</span>
4.5
</pre>
<p>A larger class of block functions can be translated just by adding parentheses and <code><span class='Modifier'>˙</span></code> (there's a discussion of this technique in APL <a href="https://dfns.dyalog.com/n_tacit.htm">here</a>). It's helpful when writing tacit code to know that <code><span class='Function'>Fn</span><span class='Modifier2'>∘</span><span class='Function'>⊣</span></code> applies <code><span class='Function'>Fn</span></code> to the left argument only and <code><span class='Function'>Fn</span><span class='Modifier2'>∘</span><span class='Function'>⊢</span></code> applies it to the right argument—these can be read "Fn of left" and "Fn of right".</p>
<h2 id="one-more-thing"><a class="header" href="#one-more-thing">One more thing</a></h2>
<p>You've probably seen <code><span class='Function'>⊢</span></code> used in documentation to display the value of a variable being assigned. Normally <code><span class='Function'>•Show</span></code> is used to display values, but the website is sort of a weird context: it displays by default but disables it if the final thing done is an assignment. <code><span class='Function'>⊢</span></code> isn't assignment, so it works arround that rule.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICJzaG93IHRoaXMi">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>a</span> <span class='Gets'>←</span> <span class='String'>"show this"</span>
"show this"
</pre>
|