aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/replicate.html
blob: 1e1fabc50365078499b4d17b10b0e2e6c03bba23 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<head>
  <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/>
  <link href="../style.css" rel="stylesheet"/>
  <title>BQN: Indices and Replicate</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="indices-and-replicate"><a class="header" href="#indices-and-replicate">Indices and Replicate</a></h1>
<svg viewBox='-134.4 -37.4 595.2 198'>
  <g font-family='BQN,monospace' font-size='18px' text-anchor='middle'>
    <rect class='code' stroke-width='1.5' rx='12' x='-100.8' y='-28.6' width='528' height='180.4'/>
    <g class='bluegreen' stroke-width='0' opacity='0.2'>
      <rect x='-91.2' y='-15.4' width='508.8' height='30.8'/>
      <rect x='-91.2' y='77' width='508.8' height='30.8'/>
    </g>
    <g font-size='24px' text-anchor='end'>
      <text dy='0.29em' x='-33.6' y='0'><tspan class='Value'>𝕩</tspan></text>
      <text dy='0.29em' x='-33.6' y='35.2'><tspan class='Value'>𝕨</tspan></text>
      <text dy='0.29em' x='-33.6' y='92.4'><tspan class='Value'>𝕨</tspan><tspan class='Function'>/</tspan><tspan class='Value'>𝕩</tspan></text>
      <text dy='0.29em' x='-33.6' y='127.6'><tspan class='Function'>/</tspan><tspan class='Value'>𝕨</tspan></text>
    </g>
    <text dy='0.29em' x='0' y='0'><tspan class='String'>'r'</tspan></text>
    <text dy='0.29em' x='48' y='0'><tspan class='String'>'e'</tspan></text>
    <text dy='0.29em' x='96' y='0'><tspan class='String'>'p'</tspan></text>
    <text dy='0.29em' x='144' y='0'><tspan class='String'>'l'</tspan></text>
    <text dy='0.29em' x='192' y='0'><tspan class='String'>'i'</tspan></text>
    <text dy='0.29em' x='240' y='0'><tspan class='String'>'c'</tspan></text>
    <text dy='0.29em' x='288' y='0'><tspan class='String'>'a'</tspan></text>
    <text dy='0.29em' x='336' y='0'><tspan class='String'>'t'</tspan></text>
    <text dy='0.29em' x='384' y='0'><tspan class='String'>'e'</tspan></text>
    <text dy='0.29em' x='0' y='35.2'><tspan class='Number'>0</tspan></text>
    <text dy='0.29em' x='48' y='35.2'><tspan class='Number'>1</tspan></text>
    <text dy='0.29em' x='96' y='35.2'><tspan class='Number'>1</tspan></text>
    <text dy='0.29em' x='144' y='35.2'><tspan class='Number'>0</tspan></text>
    <text dy='0.29em' x='192' y='35.2'><tspan class='Number'>3</tspan></text>
    <text dy='0.29em' x='240' y='35.2'><tspan class='Number'>2</tspan></text>
    <text dy='0.29em' x='288' y='35.2'><tspan class='Number'>0</tspan></text>
    <text dy='0.29em' x='336' y='35.2'><tspan class='Number'>0</tspan></text>
    <text dy='0.29em' x='384' y='35.2'><tspan class='Number'>0</tspan></text>
    <text dy='0.29em' x='0' y='92.4'><tspan class='String'>'e'</tspan></text>
    <text dy='0.29em' x='48' y='92.4'><tspan class='String'>'p'</tspan></text>
    <text dy='0.29em' x='96' y='92.4'><tspan class='String'>'i'</tspan></text>
    <text dy='0.29em' x='144' y='92.4'><tspan class='String'>'i'</tspan></text>
    <text dy='0.29em' x='192' y='92.4'><tspan class='String'>'i'</tspan></text>
    <text dy='0.29em' x='240' y='92.4'><tspan class='String'>'c'</tspan></text>
    <text dy='0.29em' x='288' y='92.4'><tspan class='String'>'c'</tspan></text>
    <text dy='0.29em' x='0' y='127.6'><tspan class='Number'>1</tspan></text>
    <text dy='0.29em' x='48' y='127.6'><tspan class='Number'>2</tspan></text>
    <text dy='0.29em' x='96' y='127.6'><tspan class='Number'>4</tspan></text>
    <text dy='0.29em' x='144' y='127.6'><tspan class='Number'>4</tspan></text>
    <text dy='0.29em' x='192' y='127.6'><tspan class='Number'>4</tspan></text>
    <text dy='0.29em' x='240' y='127.6'><tspan class='Number'>5</tspan></text>
    <text dy='0.29em' x='288' y='127.6'><tspan class='Number'>5</tspan></text>
    <g class='lilac' stroke-linecap='round'>
      <g stroke-width='0.6'>
        <line x1='0' x2='0' y1='9.9' y2='23.1'/>
        <line x1='144' x2='144' y1='9.9' y2='23.1'/>
        <line x1='288' x2='288' y1='9.9' y2='23.1'/>
        <line x1='336' x2='336' y1='9.9' y2='23.1'/>
        <line x1='384' x2='384' y1='9.9' y2='23.1'/>
      </g>
      <g stroke-width='1.8'>
        <line x1='48' x2='48' y1='9.9' y2='23.1'/>
        <line x1='96' x2='96' y1='9.9' y2='23.1'/>
        <line x1='192' x2='192' y1='9.9' y2='23.1'/>
        <line x1='192' x2='192' y1='9.9' y2='23.1'/>
        <line x1='192' x2='192' y1='9.9' y2='23.1'/>
        <line x1='240' x2='240' y1='9.9' y2='23.1'/>
        <line x1='240' x2='240' y1='9.9' y2='23.1'/>
        <line x1='48' x2='2.4' y1='45.1' y2='80.3'/>
        <line x1='96' x2='50.4' y1='45.1' y2='80.3'/>
        <line x1='192' x2='100.8' y1='45.1' y2='80.3'/>
        <line x1='192' x2='146.4' y1='45.1' y2='80.3'/>
        <line x1='192' x2='192' y1='45.1' y2='80.3'/>
        <line x1='240' x2='240' y1='45.1' y2='80.3'/>
        <line x1='240' x2='285.6' y1='45.1' y2='80.3'/>
        <line x1='0' x2='0' y1='102.3' y2='115.5'/>
        <line x1='48' x2='48' y1='102.3' y2='115.5'/>
        <line x1='96' x2='96' y1='102.3' y2='115.5'/>
        <line x1='144' x2='144' y1='102.3' y2='115.5'/>
        <line x1='192' x2='192' y1='102.3' y2='115.5'/>
        <line x1='240' x2='240' y1='102.3' y2='115.5'/>
        <line x1='288' x2='288' y1='102.3' y2='115.5'/>
      </g>
    </g>
  </g>
</svg>

<p>The functions Indices and Replicate are used to copy or filter data. They might be described as transforming a <a href="https://en.wikipedia.org/wiki/Run-length_encoding">run-length encoding</a> into unencoded form. On the other hand, Indices might be described as giving a sparse representation of <code><span class='Value'>𝕩</span></code>, which is smaller if <code><span class='Value'>𝕩</span></code> mostly consists of zeros.</p>
<p>BQN doesn't have any of the various features used in APL to add fills to the result of Replicate, like negative numbers in <code><span class='Value'>𝕨</span></code> or an Expand (<code><span class='Value'>\</span></code>) primitive. An alternative to Expand is to use Replicate with structural <a href="under.html">Under</a> (<code><span class='Modifier2'></span></code>) to insert values into an array of fills.</p>
<h2 id="replicate"><a class="header" href="#replicate">Replicate</a></h2>
<p>Given a list of natural numbers <code><span class='Value'>𝕨</span></code>, Replicate repeats each <a href="array.html#cells">major cell</a> in <code><span class='Value'>𝕩</span></code> the corresponding number of times. That is, <code><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕩</span></code> must have the same length, and the result includes <code><span class='Value'>i</span><span class='Function'></span><span class='Value'>𝕨</span></code> copies of each cell <code><span class='Value'>i</span><span class='Function'></span><span class='Value'>𝕩</span></code>, in order.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MuKAvzHigL8w4oC/MiAvICJhYmNkIgoK4oqiIGEg4oaQID4iYWEwIuKAvyJiYjEi4oC/ImNjMiLigL8iZGQzIgoKMuKAvzHigL8w4oC/MiAvIGE=">↗️</a><pre>    <span class='Number'>2</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'>2</span> <span class='Function'>/</span> <span class='String'>&quot;abcd&quot;</span>
"aabdd"

    <span class='Function'></span> <span class='Value'>a</span> <span class='Gets'></span> <span class='Function'>&gt;</span><span class='String'>&quot;aa0&quot;</span><span class='Ligature'></span><span class='String'>&quot;bb1&quot;</span><span class='Ligature'></span><span class='String'>&quot;cc2&quot;</span><span class='Ligature'></span><span class='String'>&quot;dd3&quot;</span>
┌─     
╵"aa0  
  bb1  
  cc2  
  dd3" 
      ┘

    <span class='Number'>2</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'>2</span> <span class='Function'>/</span> <span class='Value'>a</span>
┌─     
╵"aa0  
  aa0  
  bb1  
  dd3  
  dd3" 
      ┘
</pre>
<p>It's also allowed for <code><span class='Value'>𝕨</span></code> to be a single number (or enclosed number: it just needs to be a unit and not a list). In this case every cell of <code><span class='Value'>𝕩</span></code> is repeated that number of times.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyAvICJjb3B5Ig==">↗️</a><pre>    <span class='Number'>3</span> <span class='Function'>/</span> <span class='String'>&quot;copy&quot;</span>
"cccooopppyyy"
</pre>
<p>When <code><span class='Value'>𝕨</span></code> is a list of booleans, a cell is never repeated more than once, meaning that each cell of <code><span class='Value'>𝕩</span></code> is either left out (0), or kept in (1). If <code><span class='Function'>Fn</span></code> is a function with a boolean result, <code><span class='Function'>Fn</span><span class='Modifier'>¨</span><span class='Modifier2'></span><span class='Function'>/</span></code> filters elements of a list according to <code><span class='Function'>Fn</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MeKAvzHigL8w4oC/MOKAvzHigL8wIC8gImZpbHRlciIKCuKJpOKfnCdpJyAiZmlsdGVyIgoK4omk4p+cJ2kn4oq4LyAiZmlsdGVyIg==">↗️</a><pre>    <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='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>0</span> <span class='Function'>/</span> <span class='String'>&quot;filter&quot;</span>
"fie"

    <span class='Function'></span><span class='Modifier2'></span><span class='String'>'i'</span> <span class='String'>&quot;filter&quot;</span>
⟨ 1 1 0 0 1 0 ⟩

    <span class='Function'></span><span class='Modifier2'></span><span class='String'>'i'</span><span class='Modifier2'></span><span class='Function'>/</span> <span class='String'>&quot;filter&quot;</span>
"fie"
</pre>
<p>Here <code><span class='Function'></span><span class='Modifier2'></span><span class='String'>'i'</span></code> is a pervasive function, so there's no need to add <code><span class='Modifier'>¨</span></code>. Similarly, to filter major cells of an array, <code><span class='Function'>Fn</span><span class='Modifier'>˘</span><span class='Modifier2'></span><span class='Function'>/</span></code> could be used, applying <code><span class='Function'>Fn</span></code> to one major cell at a time.</p>
<p>This idea extends to Replicate as well. The function below tests which input characters are double quotes, but by adding one it changes the result to 1 for each non-quote character and 2 for quotes (but source code and display also double quotes here, so the input string has only two <code><span class='String'>&quot;</span></code>s and the output has four).</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ezErJyInPfCdlal94oq4LyAiZm9yICIiZXNjYXBpbmciIiBxdW90ZXMi">↗️</a><pre>    <span class='Brace'>{</span><span class='Number'>1</span><span class='Function'>+</span><span class='String'>'&quot;'</span><span class='Function'>=</span><span class='Value'>𝕩</span><span class='Brace'>}</span><span class='Modifier2'></span><span class='Function'>/</span> <span class='String'>&quot;for &quot;&quot;escaping&quot;&quot; quotes&quot;</span>
"for """"escaping"""" quotes"
</pre>
<h3 id="compound-replicate"><a class="header" href="#compound-replicate">Compound Replicate</a></h3>
<p>If <code><span class='Value'>𝕨</span></code> has <a href="depth.html">depth</a> two, then its elements give the amounts to copy along each <a href="leading.html">leading axis</a> of <code><span class='Value'>𝕩</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDLigL81IOKliiDihpUxMAoK4p+oMuKAvzAsIDHigL8w4oC/MOKAvzHigL8x4p+pIC8gYgoKMuKAvzAgLyAx4oC/MOKAvzDigL8x4oC/MeKKuC/LmCBi">↗️</a><pre>    <span class='Function'></span> <span class='Value'>b</span> <span class='Gets'></span> <span class='Number'>2</span><span class='Ligature'></span><span class='Number'>5</span> <span class='Function'></span> <span class='Function'></span><span class='Number'>10</span>
┌─           
╵ 0 1 2 3 4  
  5 6 7 8 9  
            ┘

    <span class='Bracket'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span><span class='Separator'>,</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='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>1</span><span class='Bracket'></span> <span class='Function'>/</span> <span class='Value'>b</span>
┌─       
╵ 0 3 4  
  0 3 4  
        ┘

    <span class='Number'>2</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'>0</span><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>1</span><span class='Modifier2'></span><span class='Function'>/</span><span class='Modifier'>˘</span> <span class='Value'>b</span>
┌─       
╵ 0 3 4  
  0 3 4  
        ┘
</pre>
<p>Here the <code><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span></code> indicates that the first row of <code><span class='Value'>b</span></code> is copied twice and the second is ignored, while <code><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='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>1</span></code> picks out three entries from that row. As in the single-axis case, <code><span class='Value'>𝕩</span></code> can have extra trailing axes that aren't modified by <code><span class='Value'>𝕨</span></code>. The rules are that <code><span class='Value'>𝕨</span></code> can't have <em>more</em> elements than axes of <code><span class='Value'>𝕩</span></code> (so <code><span class='Paren'>(</span><span class='Function'></span><span class='Value'>𝕨</span><span class='Paren'>)</span><span class='Function'>≤=</span><span class='Value'>𝕩</span></code>), and that each element has to have the same length as the corresponding axis—or it can be a unit, as shown below.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oPDIsPDPin6kgLyBi">↗️</a><pre>    <span class='Bracket'></span><span class='Function'>&lt;</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Function'>&lt;</span><span class='Number'>3</span><span class='Bracket'></span> <span class='Function'>/</span> <span class='Value'>b</span>
┌─                               
╵ 0 0 0 1 1 1 2 2 2 3 3 3 4 4 4  
  0 0 0 1 1 1 2 2 2 3 3 3 4 4 4  
  5 5 5 6 6 6 7 7 7 8 8 8 9 9 9  
  5 5 5 6 6 6 7 7 7 8 8 8 9 9 9  
                                ┘
</pre>
<p>Above, both elements of <code><span class='Value'>𝕨</span></code> are <a href="enclose.html">enclosed</a> numbers. An individual element doesn't have to be enclosed, but I don't recommend this, since if <em>none</em> of them are enclosed, then <code><span class='Value'>𝕨</span></code> will have depth 1 and it will be interpreted as replicating along the first axis only.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMiwz4p+pIC8gYg==">↗️</a><pre>    <span class='Bracket'></span><span class='Number'>2</span><span class='Separator'>,</span><span class='Number'>3</span><span class='Bracket'></span> <span class='Function'>/</span> <span class='Value'>b</span>
┌─           
╵ 0 1 2 3 4  
  0 1 2 3 4  
  5 6 7 8 9  
  5 6 7 8 9  
  5 6 7 8 9  
            ┘
</pre>
<p>The example above has a different result from the previous one! The function <code><span class='Function'>&lt;</span><span class='Modifier'>¨</span><span class='Modifier2'></span><span class='Function'>/</span></code> could be used in place of <code><span class='Function'>/</span></code> to replicate each axis by a different number.</p>
<p>If <code><span class='Value'>𝕨</span></code> is <code><span class='Bracket'>⟨⟩</span></code>, then it has depth 1, but is handled with the multidimensional case anyway, giving a result of <code><span class='Value'>𝕩</span></code>. The one-dimensional case could also only ever return <code><span class='Value'>𝕩</span></code>, but it would be required to have length 0, so this convention still only extends the simple case.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YiDiiaEg4p+o4p+pIC8gYg==">↗️</a><pre>    <span class='Value'>b</span> <span class='Function'></span> <span class='Bracket'>⟨⟩</span> <span class='Function'>/</span> <span class='Value'>b</span>
1
</pre>
<h2 id="indices"><a class="header" href="#indices">Indices</a></h2>
<p>The monadic form of <code><span class='Function'>/</span></code> is much simpler than the dyadic one, with no multidimensional case or mismatched argument ranks. <code><span class='Value'>𝕩</span></code> has to be a list of natural numbers, and <code><span class='Function'>/</span><span class='Value'>𝕩</span></code> is the list <code><span class='Value'>𝕩</span><span class='Function'>/↕≠</span><span class='Value'>𝕩</span></code>. Its elements are the <a href="indices.html">indices</a> for <code><span class='Value'>𝕩</span></code>, with index <code><span class='Value'>i</span></code> repeated <code><span class='Value'>i</span><span class='Function'></span><span class='Value'>𝕩</span></code> times.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LyAz4oC/MOKAvzLigL8x">↗️</a><pre>    <span class='Function'>/</span> <span class='Number'>3</span><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>1</span>
⟨ 0 0 0 2 2 3 ⟩
</pre>
<p>A unit argument isn't allowed, and isn't very useful: for example, <code><span class='Function'>/</span><span class='Number'>6</span></code> might indicate an array of six zeros, but this can be written <code><span class='Function'>/⥊</span><span class='Number'>6</span></code> or <code><span class='Number'>6</span><span class='Function'></span><span class='Number'>0</span></code> with hardly any extra effort.</p>
<p>When <code><span class='Value'>𝕨</span></code> has rank 1, <code><span class='Value'>𝕨</span><span class='Function'>/</span><span class='Value'>𝕩</span></code> is equivalent to <code><span class='Value'>𝕨</span><span class='Function'>/</span><span class='Modifier2'></span><span class='Function'></span><span class='Value'>𝕩</span></code>. Of course, this isn't the only use of Indices. It also gets along well with <a href="group.html">Group</a>: for example, <code><span class='Function'>/</span><span class='Modifier2'></span><span class='Function'></span></code> groups <code><span class='Value'>𝕩</span></code> according to a list of lengths <code><span class='Value'>𝕨</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MuKAvzXigL8w4oC/MSAv4oq44oqUICJBQkNERUZHSCI=">↗️</a><pre>    <span class='Number'>2</span><span class='Ligature'></span><span class='Number'>5</span><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>1</span> <span class='Function'>/</span><span class='Modifier2'></span><span class='Function'></span> <span class='String'>&quot;ABCDEFGH&quot;</span>
⟨ "AB" "CDEFG" ⟨⟩ "H" ⟩
</pre>
<p>This function will fail to include trailing empty arrays; the modification <code><span class='Paren'>(</span><span class='Function'>/∾</span><span class='Modifier2'></span><span class='Number'>1</span><span class='Paren'>)</span><span class='Modifier2'></span><span class='Function'></span></code> fixes this and ensures the result always has as many elements as <code><span class='Value'>𝕨</span></code>.</p>
<p>If <code><span class='Value'>𝕩</span></code> is boolean then <code><span class='Function'>/</span><span class='Value'>𝕩</span></code> contains all the indices where a 1 appears in <code><span class='Value'>𝕩</span></code>. Applying <code><span class='Function'>-</span><span class='Modifier2'></span><span class='Function'>»</span></code> to the result gives the distance from each 1 to the <a href="shift.html">previous</a>, or to the start of the list, another potentially useful function.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LyAw4oC/MeKAvzDigL8x4oC/MOKAvzDigL8w4oC/MOKAvzHigL8wCgot4p+cwrsgLyAw4oC/MeKAvzDigL8x4oC/MOKAvzDigL8w4oC/MOKAvzHigL8w">↗️</a><pre>    <span class='Function'>/</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><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>0</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>
⟨ 1 3 8 ⟩

    <span class='Function'>-</span><span class='Modifier2'></span><span class='Function'>»</span> <span class='Function'>/</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><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>0</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>
⟨ 1 2 5 ⟩
</pre>
<p>With more effort we can also use <code><span class='Function'>/</span></code> to analyze groups of 1s in the argument (and of course all these methods can be applied to 0s instead, by first flipping the values with <code><span class='Function'>¬</span></code>). First we highlight the start and end of each group by comparing the list with a shifted copy of itself. Or rather, we'll first place a 0 at the front and then at the end, in order to detect when a group starts at the beginning of the list or ends at the end (there's also a shift-based version, <code><span class='Function'></span><span class='Modifier2'></span><span class='Function'>«</span><span class='Number'>0</span><span class='Function'></span><span class='Value'>𝕩</span></code>).</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MCAo4oi+4omN4oi+y5wpIDDigL8x4oC/MeKAvzHigL8w4oC/MOKAvzHigL8w4oC/MeKAvzHigL8wCgowICjiiL7iiaDiiL7LnCkgMOKAvzHigL8x4oC/MeKAvzDigL8w4oC/MeKAvzDigL8x4oC/MeKAvzAKCi8gMCjiiL7iiaDiiL7LnCkgMOKAvzHigL8x4oC/MeKAvzDigL8w4oC/MeKAvzDigL8x4oC/MeKAvzA=">↗️</a><pre>    <span class='Number'>0</span> <span class='Paren'>(</span><span class='Function'>∾≍∾</span><span class='Modifier'>˜</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'>1</span><span class='Ligature'></span><span class='Number'>0</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><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>0</span>
┌─                         
╵ 0 0 1 1 1 0 0 1 0 1 1 0  
  0 1 1 1 0 0 1 0 1 1 0 0  
                          ┘

    <span class='Number'>0</span> <span class='Paren'>(</span><span class='Function'>∾≠∾</span><span class='Modifier'>˜</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'>1</span><span class='Ligature'></span><span class='Number'>0</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><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>0</span>
⟨ 0 1 0 0 1 0 1 1 1 0 1 0 ⟩

    <span class='Function'>/</span> <span class='Number'>0</span><span class='Paren'>(</span><span class='Function'>∾≠∾</span><span class='Modifier'>˜</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'>1</span><span class='Ligature'></span><span class='Number'>0</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><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>0</span>
⟨ 1 4 6 7 8 10 ⟩
</pre>
<p>So now we have the indices of each transition from 0 to 1 or 1 to 0, in an extended list with 0 added at the beginning and end. The first index has to be for a 0 to 1 transition, because we forced the first value to be a 0, and then the next can only be 1 to 0, then 0 to 1, and so on until the last, which must be 1 to 0 because the last value is also 0.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LcucYMuYIOKImOKAvzLipYovIDAo4oi+4omg4oi+y5wpIDDigL8x4oC/MeKAvzHigL8w4oC/MOKAvzHigL8w4oC/MeKAvzHigL8w">↗️</a><pre>    <span class='Function'>-</span><span class='Modifier'>˜`˘</span> <span class='Modifier2'></span><span class='Ligature'></span><span class='Number'>2</span><span class='Function'>⥊/</span> <span class='Number'>0</span><span class='Paren'>(</span><span class='Function'>∾≠∾</span><span class='Modifier'>˜</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'>1</span><span class='Ligature'></span><span class='Number'>0</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><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>0</span>
┌─     
╵ 1 3  
  6 1  
  8 2  
      ┘
</pre>
<p>This means the transitions can be grouped exactly in pairs, the beginning and end of each group. Reshape with a <a href="reshape.html#computed-lengths">computed length</a> <code><span class='Modifier2'></span><span class='Ligature'></span><span class='Number'>2</span></code> groups these pairs, and then a <a href="scan.html">scan</a> <code><span class='Function'>-</span><span class='Modifier'>˜`˘</span></code> can be used to convert the start/end format to start/length if wanted.</p>
<h3 id="inverse"><a class="header" href="#inverse">Inverse</a></h3>
<p>The result of Indices <code><span class='Function'>/</span><span class='Value'>n</span></code> is an ordered list of natural numbers, where the number <code><span class='Value'>i</span></code> appears <code><span class='Value'>i</span><span class='Function'></span><span class='Value'>n</span></code> times. Given an ordered list of natural numbers <code><span class='Value'>k</span></code>, the <a href="undo.html"><em>inverse</em></a> of indices returns a corresponding <code><span class='Value'>n</span></code>: one where the value <code><span class='Value'>i</span><span class='Function'></span><span class='Value'>n</span></code> is the number of times <code><span class='Value'>i</span></code> appears in <code><span class='Value'>k</span></code>.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LyAz4oC/MuKAvzEKCi/igbwgMOKAvzDigL8w4oC/MeKAvzHigL8y">↗️</a><pre>    <span class='Function'>/</span> <span class='Number'>3</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>1</span>
⟨ 0 0 0 1 1 2 ⟩

    <span class='Function'>/</span><span class='Modifier'></span> <span class='Number'>0</span><span class='Ligature'></span><span class='Number'>0</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'>1</span><span class='Ligature'></span><span class='Number'>2</span>
⟨ 3 2 1 ⟩
</pre>
<p>Finding how many times each index appears in a list of indices is often a useful thing to do, and there are a few ways to do it:</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8udy5ggKOKGlTUpID3ijJwgMuKAvzLigL804oC/MeKAvzLigL8wICAjIEluZWZmaWNpZW50CgriiaDCqOKKlCAy4oC/MuKAvzTigL8x4oC/MuKAvzAKCi/igbwgMuKAvzLigL804oC/MeKAvzLigL8w">↗️</a><pre>    <span class='Function'>+</span><span class='Modifier'>˝˘</span> <span class='Paren'>(</span><span class='Function'></span><span class='Number'>5</span><span class='Paren'>)</span> <span class='Function'>=</span><span class='Modifier'></span> <span class='Number'>2</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>4</span><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span>  <span class='Comment'># Inefficient
</span>⟨ 1 1 3 0 1 ⟩

    <span class='Function'></span><span class='Modifier'>¨</span><span class='Function'></span> <span class='Number'>2</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>4</span><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span>
⟨ 1 1 3 0 1 ⟩

    <span class='Function'>/</span><span class='Modifier'></span> <span class='Number'>2</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>4</span><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span>
⟨ 1 1 3 0 1 ⟩
</pre>
<p>The last of these is an extension defined in the language specification. As we said, the result of Indices is always sorted, so properly there's no argument that could return <code><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>4</span><span class='Ligature'></span><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>2</span><span class='Ligature'></span><span class='Number'>0</span></code>. But the index-counting function is very useful, so <code><span class='Function'>/</span><span class='Modifier'></span></code> is defined to implicitly sort its argument (which is still required to be a list of natural numbers). Since <code><span class='Function'>/</span><span class='Modifier'></span></code> is implemented as a single operation, it's the best way to perform this counting task.</p>
<h3 id="just-rank-1"><a class="header" href="#just-rank-1">Just rank 1?</a></h3>
<p>So if <code><span class='Value'>𝕩</span></code> is a boolean list, <code><span class='Function'>/</span><span class='Value'>𝕩</span></code> tells you where each 1 is located. What about a boolean array?</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHIg4oaQIDHCqOKMvigz4oC/OOKKj+KliikgM+KAvzbipYowCi9y">↗️</a><pre>    <span class='Function'></span> <span class='Value'>r</span> <span class='Gets'></span> <span class='Number'>1</span><span class='Modifier'>¨</span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Number'>3</span><span class='Ligature'></span><span class='Number'>8</span><span class='Function'>⊏⥊</span><span class='Paren'>)</span> <span class='Number'>3</span><span class='Ligature'></span><span class='Number'>6</span><span class='Function'></span><span class='Number'>0</span>
┌─             
╵ 0 0 0 1 0 0  
  0 0 1 0 0 0  
  0 0 0 0 0 0  
              ┘
    <span class='Function'>/</span><span class='Value'>r</span>
<span class='Error'>Error: /: Argument must have rank 1 (3‿6 ≡ ≢𝕩)</span>
</pre>
<p>Error. But the two 1s are located at <code><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>3</span></code> and <code><span class='Number'>1</span><span class='Ligature'></span><span class='Number'>2</span></code>. What's wrong with those? First, let's note that you can find these indices if you need to, using Replicate. Make the list of all indices <code><span class='Function'>↕≢</span><span class='Value'>𝕩</span></code>, and filter <a href="compose.html#over">Over</a> <a href="reshape.html#deshape">Deshape</a> (<code><span class='Modifier2'></span><span class='Function'></span></code>).</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=L+KXi+KliuKfnCjihpXiiaIpIHI=">↗️</a><pre>    <span class='Function'>/</span><span class='Modifier2'></span><span class='Function'></span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Function'>↕≢</span><span class='Paren'>)</span> <span class='Value'>r</span>
⟨ ⟨ 0 3 ⟩ ⟨ 1 2 ⟩ ⟩
</pre>
<p>The issue with this function is that it's not consistent with the result of <code><span class='Function'>/</span></code> on a list. This is because the extension gives <a href="indices.html#element-indices">element indices</a>, which are lists, while the original <code><span class='Function'>/</span></code> gives single-number indices, which is only possible when <code><span class='Value'>𝕩</span></code> has rank 1.</p>
<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=L+KXi+KliuKfnCjihpXiiaIpIDDigL8x4oC/MOKAvzHigL8w4oC/MOKAvzDigL8w4oC/MeKAvzAKCi8gICAgICAgIDDigL8x4oC/MOKAvzHigL8w4oC/MOKAvzDigL8w4oC/MeKAvzA=">↗️</a><pre>    <span class='Function'>/</span><span class='Modifier2'></span><span class='Function'></span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Function'>↕≢</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'>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'>0</span><span class='Ligature'></span><span class='Number'>0</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>
⟨ ⟨ 1 ⟩ ⟨ 3 ⟩ ⟨ 8 ⟩ ⟩

    <span class='Function'>/</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><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>0</span><span class='Ligature'></span><span class='Number'>0</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>
⟨ 1 3 8 ⟩
</pre>
<p>So these functions can't be the same primitive. The only thing we can do—what some APLs do—is to use index lists when <code><span class='Value'>𝕩</span></code> has rank not equal to 1 but index numbers otherwise. But supporting a partial function is hazardous: it means that code that works for ranks 4, 3, 2… might not work for rank 1. This isn't how BQN does things. So if you need this functionality, you need to spell it out.</p>
<p>Which isn't too simple? The other part of the story, and why I think <code><span class='Function'>/</span><span class='Modifier2'></span><span class='Function'></span><span class='Modifier2'></span><span class='Paren'>(</span><span class='Function'>↕≢</span><span class='Paren'>)</span></code> is good enough despite its length, is that I just don't like element indices. They force array nesting here, which is complicated and slow. There's usually a better approach, such as using arithmetic to skip indices entirely, or <code><span class='Modifier2'></span><span class='Function'></span></code> (<a href="under.html">Under</a> Deshape) to temporarily work on the array as a list.</p>