aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/identity.html
blob: 520e1657d9a645c1239d15181ba330f6083e7886 (plain)
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
80
81
82
83
84
85
86
<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'>&quot;only&quot;</span>
"only"

    <span class='Function'></span> <span class='String'>&quot;only&quot;</span>
"only"

    <span class='String'>&quot;left&quot;</span> <span class='Function'></span> <span class='String'>&quot;right&quot;</span>
"right"

    <span class='String'>&quot;left&quot;</span> <span class='Function'></span> <span class='String'>&quot;right&quot;</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 tacit 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'>&quot;ABCDE&quot;</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'>&quot;abcde&quot;</span>
"aBCde"

    <span class='Bracket'></span><span class='String'>&quot;wxy&quot;</span><span class='Ligature'></span><span class='String'>&quot;z&quot;</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'>&lt;</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 &quot;Fn of left&quot; and &quot;Fn of right&quot;.</p>
<h2 id="syntax-tricks"><a class="header" href="#syntax-tricks">Syntax tricks</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. This is a hack, and in most contexts <code><span class='Function'>•Show</span></code> should be used to display values.</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'>&quot;show this&quot;</span>
"show this"
</pre>
<p>More importantly, <code><span class='Modifier2'></span><span class='Function'></span></code> can be used to ignore a right argument for modified assignment, to apply a function &quot;in place&quot; to a variable without writing the variable name twice.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDijL3iiJjiiqPihqkgQA==">↗️</a><pre>    <span class='Value'>a</span> <span class='Function'></span><span class='Modifier2'></span><span class='Function'></span><span class='Gets'></span> <span class='String'>@</span>
"siht wohs"
</pre>
<p>In APL a tack can be used to avoid stranding numbers together. In BQN, stranding is explicit, and there's no need!</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=w7fin5wy4o2fM+KKoiAyNArDt+KfnDLijZ8zIDI0">↗️</a><pre>    <span class='Function'>÷</span><span class='Modifier2'></span><span class='Number'>2</span><span class='Modifier2'></span><span class='Number'>3</span><span class='Function'></span> <span class='Number'>24</span>
3
    <span class='Function'>÷</span><span class='Modifier2'></span><span class='Number'>2</span><span class='Modifier2'></span><span class='Number'>3</span> <span class='Number'>24</span>
3
</pre>
<p>(Wow, what a useless section.)</p>