diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-07-14 20:06:50 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2022-07-14 20:06:50 -0400 |
| commit | e2b07a5fd0bbaad232c717fb90a31d6c61d72bd4 (patch) | |
| tree | 8cd1d594838a87f4257e73d1e0e32d69ec4a148c /docs | |
| parent | fe13f4a775a83073cb44d8e47ec4fdf9ec957bed (diff) | |
Try to include previous variable definitions in REPL links
Diffstat (limited to 'docs')
35 files changed, 118 insertions, 118 deletions
diff --git a/docs/doc/block.html b/docs/doc/block.html index ec9e4ed9..cfffe895 100644 --- a/docs/doc/block.html +++ b/docs/doc/block.html @@ -176,7 +176,7 @@ <p>Unlike these assignments, the header also constrains what inputs the block can take: a monadic 1-modifier like the one above can't take a right operand or left argument, so its body can't contain <code><span class='Function'>𝔾</span></code> or <code><span class='Value'>𝕨</span></code>. Calling it with a left argument, or a right argument that isn't a two-element list, will result in an error.</p> <h3 id="destructuring"><a class="header" href="#destructuring">Destructuring</a></h3> <p>Arguments and operands allow <a href="expression.html#destructuring">destructuring</a> like assignment does. While assignment only tolerates lists of variables, header destructuring also allows constants. For the header to match, the argument must share the given structure, including the constants where they appear.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RGVzdHJ1Y3Qg4oaQIHsg8J2ViiBh4oC/MeKAv+KfqGIswrcsMuKfqTogYeKJjWIgfQpEZXN0cnVjdCAgICAgICA14oC/MeKAv+KfqDcsz4AsMuKfqQ==">↗️</a><pre> <span class='Function'>Destruct</span> <span class='Gets'>←</span> <span class='Brace'>{</span> <span class='Function'>𝕊</span> <span class='Value'>a</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Bracket'>⟨</span><span class='Value'>b</span><span class='Separator'>,</span><span class='Nothing'>·</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Bracket'>⟩</span><span class='Head'>:</span> <span class='Value'>a</span><span class='Function'>≍</span><span class='Value'>b</span> <span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YeKGkGLihpAib3V0ZXIiCkRlc3RydWN0IOKGkCB7IPCdlYogYeKAvzHigL/in6hiLMK3LDLin6k6IGHiiY1iIH0KRGVzdHJ1Y3QgICAgICAgNeKAvzHigL/in6g3LM+ALDLin6k=">↗️</a><pre> <span class='Function'>Destruct</span> <span class='Gets'>←</span> <span class='Brace'>{</span> <span class='Function'>𝕊</span> <span class='Value'>a</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Bracket'>⟨</span><span class='Value'>b</span><span class='Separator'>,</span><span class='Nothing'>·</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Bracket'>⟩</span><span class='Head'>:</span> <span class='Value'>a</span><span class='Function'>≍</span><span class='Value'>b</span> <span class='Brace'>}</span> <span class='Function'>Destruct</span> <span class='Number'>5</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Bracket'>⟨</span><span class='Number'>7</span><span class='Separator'>,</span><span class='Number'>π</span><span class='Separator'>,</span><span class='Number'>2</span><span class='Bracket'>⟩</span> ⟨ 5 7 ⟩ </pre> @@ -219,12 +219,12 @@ ⟨ 2 4 ⟩ </pre> <p>If no header is compatible, the call results in an error.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyBDYXNlQWRkIDM=">↗️</a><pre> <span class='Number'>3</span> <span class='Function'>CaseAdd</span> <span class='Number'>3</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Q2FzZUFkZCDihpAgewogIDLwnZWKMzogMOKAvzUgOwogIDLwnZWK8J2VqTog4p+oMSwyK/Cdlanin6kgOwogICDwnZWK8J2VqTogMuKAv/CdlakKfQozIENhc2VBZGQgMw==">↗️</a><pre> <span class='Number'>3</span> <span class='Function'>CaseAdd</span> <span class='Number'>3</span> <span class='Error'>Error: No header matched arguments</span> </pre> <h3 id="case-headers"><a class="header" href="#case-headers">Case headers</a></h3> <p>A special rule allows for convenient case-matching syntax for one-argument functions. In any function header with one argument, the function name can be omitted as long as the argument is <em>not</em> a plain identifier—it must be <code><span class='Value'>𝕩</span></code> or a compound value like a list to distinguish it from an immediate block label.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=VGVzdCDihpAgewogICJhYmMiOiAic3RyaW5nIiA7CiAg4p+oMixi4p+pOiDijL3wnZWpICAgICAgIDsKICA1OiAgICAgIm51bWJlciIgOwogIPCdlak6ICAgICAiZGVmYXVsdCIKfQpUZXN0IDU=">↗️</a><pre> <span class='Function'>Test</span> <span class='Gets'>←</span> <span class='Brace'>{</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YeKGkGLihpAib3V0ZXIiClRlc3Qg4oaQIHsKICAiYWJjIjogInN0cmluZyIgOwogIOKfqDIsYuKfqTog4oy98J2VqSAgICAgICA7CiAgNTogICAgICJudW1iZXIiIDsKICDwnZWpOiAgICAgImRlZmF1bHQiCn0KVGVzdCA1">↗️</a><pre> <span class='Function'>Test</span> <span class='Gets'>←</span> <span class='Brace'>{</span> <span class='String'>"abc"</span><span class='Head'>:</span> <span class='String'>"string"</span> <span class='Head'>;</span> <span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Value'>b</span><span class='Bracket'>⟩</span><span class='Head'>:</span> <span class='Function'>⌽</span><span class='Value'>𝕩</span> <span class='Head'>;</span> <span class='Number'>5</span><span class='Head'>:</span> <span class='String'>"number"</span> <span class='Head'>;</span> @@ -236,7 +236,7 @@ <p>These case-style headers function exactly the same as if they were preceded by <code><span class='Function'>𝕊</span></code>, and can be mixed with other kinds of headers.</p> <h3 id="predicates"><a class="header" href="#predicates">Predicates</a></h3> <p>Destructuring with a header is limited, as it can only match a particular structure or value exactly—not, for example, a range of lengths. A predicate, written with <code><span class='Head'>?</span></code>, allows you to test an arbitrary property before evaluating the rest of the body, and also serves as a limited kind of control flow. It can be thought of as an extension to a header. So the following function requires the argument to have two elements and for the first to be less than the second before using the first body. Otherwise it moves to the next body, which is unconditional.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Q2hlY2tQYWlyIOKGkCB7IPCdlYrin6hhLGLin6k6IGE8Yj8gIm9rIiA7ICJub3Qgb2siIH0KCkNoZWNrUGFpciDin6gzLDjin6kgICAgIyBGYWlscyBkZXN0cnVjdHVyaW5nCgpDaGVja1BhaXIg4p+oMSw0LDXin6kgICMgTm90IGEgcGFpcgoKQ2hlY2tQYWlyIOKfqDMswq8x4p+pICAgIyBOb3QgYXNjZW5kaW5n">↗️</a><pre> <span class='Function'>CheckPair</span> <span class='Gets'>←</span> <span class='Brace'>{</span> <span class='Function'>𝕊</span><span class='Bracket'>⟨</span><span class='Value'>a</span><span class='Separator'>,</span><span class='Value'>b</span><span class='Bracket'>⟩</span><span class='Head'>:</span> <span class='Value'>a</span><span class='Function'><</span><span class='Value'>b</span><span class='Head'>?</span> <span class='String'>"ok"</span> <span class='Head'>;</span> <span class='String'>"not ok"</span> <span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YeKGkGLihpAib3V0ZXIiCkNoZWNrUGFpciDihpAgeyDwnZWK4p+oYSxi4p+pOiBhPGI/ICJvayIgOyAibm90IG9rIiB9CgpDaGVja1BhaXIg4p+oMyw44p+pICAgICMgRmFpbHMgZGVzdHJ1Y3R1cmluZwoKQ2hlY2tQYWlyIOKfqDEsNCw14p+pICAjIE5vdCBhIHBhaXIKCkNoZWNrUGFpciDin6gzLMKvMeKfqSAgICMgTm90IGFzY2VuZGluZw==">↗️</a><pre> <span class='Function'>CheckPair</span> <span class='Gets'>←</span> <span class='Brace'>{</span> <span class='Function'>𝕊</span><span class='Bracket'>⟨</span><span class='Value'>a</span><span class='Separator'>,</span><span class='Value'>b</span><span class='Bracket'>⟩</span><span class='Head'>:</span> <span class='Value'>a</span><span class='Function'><</span><span class='Value'>b</span><span class='Head'>?</span> <span class='String'>"ok"</span> <span class='Head'>;</span> <span class='String'>"not ok"</span> <span class='Brace'>}</span> <span class='Function'>CheckPair</span> <span class='Bracket'>⟨</span><span class='Number'>3</span><span class='Separator'>,</span><span class='Number'>8</span><span class='Bracket'>⟩</span> <span class='Comment'># Fails destructuring </span>"ok" diff --git a/docs/doc/constant.html b/docs/doc/constant.html index d68235aa..258ae26b 100644 --- a/docs/doc/constant.html +++ b/docs/doc/constant.html @@ -50,6 +50,6 @@ ⟨ 1 2 ¯3 4 ⟩ </pre> <p>Here <code><span class='Value'>m</span></code> is applied to <code><span class='Number'>2</span><span class='Function'>⊑</span><span class='Value'>𝕩</span></code> even though we want to discard that value. Spelled as <code><span class='Value'>m</span></code>, our <a href="context.html">context-free grammar</a> knows it's a function argument, but this <a href="../problems.html#syntactic-type-erasure">doesn't affect</a> later usage. <a href="under.html">Under</a> always applies <code><span class='Function'>𝔽</span></code> as a function. The proper definition of the insertion function should use a <code><span class='Modifier'>˙</span></code>, like this:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=bSB78J2VqMuZ4oy+KDLiirjiipEpIPCdlal9IDHigL8y4oC/M+KAvzQ=">↗️</a><pre> <span class='Value'>m</span> <span class='Brace'>{</span><span class='Value'>𝕨</span><span class='Modifier'>˙</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>⊑</span><span class='Paren'>)</span> <span class='Value'>𝕩</span><span class='Brace'>}</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=TSDihpAgLQptIHvwnZWoy5nijL4oMuKKuOKKkSkg8J2VqX0gMeKAvzLigL8z4oC/NA==">↗️</a><pre> <span class='Value'>m</span> <span class='Brace'>{</span><span class='Value'>𝕨</span><span class='Modifier'>˙</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>2</span><span class='Modifier2'>⊸</span><span class='Function'>⊑</span><span class='Paren'>)</span> <span class='Value'>𝕩</span><span class='Brace'>}</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>4</span> ⟨ 1 2 - 4 ⟩ </pre> diff --git a/docs/doc/couple.html b/docs/doc/couple.html index 86d8434a..e583b7f6 100644 --- a/docs/doc/couple.html +++ b/docs/doc/couple.html @@ -66,7 +66,7 @@ <p>As a consequence, <a href="pair.html">Pair</a> (<code><span class='Function'>⋈</span></code>) can be written <code><span class='Function'>≍</span><span class='Modifier2'>○</span><span class='Function'><</span></code>, while <code><span class='Function'>≍</span></code> is <code><span class='Function'>></span><span class='Modifier2'>∘</span><span class='Function'>⋈</span></code> as discussed above. This gives the neat (but not useful) identities <code><span class='Function'>≍</span> <span class='Gets'>←→</span> <span class='Function'>></span><span class='Modifier2'>∘</span><span class='Function'>≍</span><span class='Modifier2'>○</span><span class='Function'><</span></code>, and <code><span class='Function'>⋈</span> <span class='Gets'>←→</span> <span class='Function'>></span><span class='Modifier2'>∘</span><span class='Function'>⋈</span><span class='Modifier2'>○</span><span class='Function'><</span></code>, which have the same form because adding <code><span class='Modifier2'>○</span><span class='Function'><</span></code> commutes with adding <code><span class='Function'>></span><span class='Modifier2'>∘</span></code>.</p> <h2 id="merge-and-array-theory"><a class="header" href="#merge-and-array-theory">Merge and array theory</a></h2> <p>In all cases, what these functions do is more like reinterpreting existing data than creating new information. In fact, if we ignore the shape and look at the deshaped arrays involved in a call to Merge, we find that it just <a href="join.html">joins</a> them together. Essentially, Merge is a request to ensure that the inner arrays make up a homogeneous (not "ragged") array, and then to consider them to be such an array. It's the same thing <a href="rank.html">Rank</a> does to combine the result cells from its operand into a single array.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4qWKID4gYQoK4qWKIOKlisKoIGEK4oi+IOKliiDipYrCqCBh">↗️</a><pre> <span class='Function'>⥊</span> <span class='Function'>></span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICJBQiLigL8iQ0QiIOKIvuKMnCAicnN0IuKAvyJ1dnci4oC/Inh5eiIK4qWKID4gYQoK4qWKIOKlisKoIGEK4oi+IOKliiDipYrCqCBh">↗️</a><pre> <span class='Function'>⥊</span> <span class='Function'>></span> <span class='Value'>a</span> "ABrstABuvwABxyzCDrstCDuvwCDxyz" <span class='Function'>⥊</span> <span class='Function'>⥊</span><span class='Modifier'>¨</span> <span class='Value'>a</span> diff --git a/docs/doc/depth.html b/docs/doc/depth.html index 596faa33..7bce4e89 100644 --- a/docs/doc/depth.html +++ b/docs/doc/depth.html @@ -162,7 +162,7 @@ 3 </pre> <p>Reversing <code><span class='Value'>n</span></code> swaps all the rows:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oy9IG4=">↗️</a><pre> <span class='Function'>⌽</span> <span class='Value'>n</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIG4g4oaQIDzijokx4o2fMiA04oC/MuKAvzLigL8z4qWK4oaVNDgK4oy9IG4=">↗️</a><pre> <span class='Function'>⌽</span> <span class='Value'>n</span> ┌─ ╵ ⟨ ⟨ 36 37 38 ⟩ ⟨ 39 40 41 ⟩ ⟩ ⟨ ⟨ 42 43 44 ⟩ ⟨ 45 46 47 ⟩ ⟩ ⟨ ⟨ 24 25 26 ⟩ ⟨ 27 28 29 ⟩ ⟩ ⟨ ⟨ 30 31 32 ⟩ ⟨ 33 34 35 ⟩ ⟩ @@ -171,7 +171,7 @@ ┘ </pre> <p>Depth <code><span class='Number'>¯1</span></code> is equivalent to Each, and reverses the larger lists, while depth <code><span class='Number'>¯2</span></code> applies Each twice to reverse the smaller lists:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oy94pqHwq8xIG4K4oy94pqHwq8yIG4=">↗️</a><pre> <span class='Function'>⌽</span><span class='Modifier2'>⚇</span><span class='Number'>¯1</span> <span class='Value'>n</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIG4g4oaQIDzijokx4o2fMiA04oC/MuKAvzLigL8z4qWK4oaVNDgK4oy94pqHwq8xIG4K4oy94pqHwq8yIG4=">↗️</a><pre> <span class='Function'>⌽</span><span class='Modifier2'>⚇</span><span class='Number'>¯1</span> <span class='Value'>n</span> ┌─ ╵ ⟨ ⟨ 3 4 5 ⟩ ⟨ 0 1 2 ⟩ ⟩ ⟨ ⟨ 9 10 11 ⟩ ⟨ 6 7 8 ⟩ ⟩ ⟨ ⟨ 15 16 17 ⟩ ⟨ 12 13 14 ⟩ ⟩ ⟨ ⟨ 21 22 23 ⟩ ⟨ 18 19 20 ⟩ ⟩ diff --git a/docs/doc/expression.html b/docs/doc/expression.html index b8f9254c..f8c40256 100644 --- a/docs/doc/expression.html +++ b/docs/doc/expression.html @@ -234,7 +234,7 @@ ⟨ ⟨ 1 0 ⟩ ⟨ 1 1 ⟩ ⟨ 1 2 ⟩ ⟩ </pre> <p>Namespace destructuring uses an overlapping syntax, fully described in <a href="namespace.html#imports">its own section</a>. The left hand side is a list of names, or aliases <code><span class='Value'>to</span><span class='Gets'>⇐</span><span class='Value'>from</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ceKAv3Ig4oapIHtx4oeQMity4oeQMC41fSDii4QgcQ==">↗️</a><pre> <span class='Value'>q</span><span class='Ligature'>‿</span><span class='Value'>r</span> <span class='Gets'>↩</span> <span class='Brace'>{</span><span class='Value'>q</span><span class='Gets'>⇐</span><span class='Number'>2</span><span class='Function'>+</span><span class='Value'>r</span><span class='Gets'>⇐</span><span class='Number'>0.5</span><span class='Brace'>}</span> <span class='Separator'>⋄</span> <span class='Value'>q</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oceKAv3Isc+KfqSDihpAg4p+oInFyIizihpU04p+pCnHigL9yIOKGqSB7ceKHkDIrcuKHkDAuNX0g4ouEIHE=">↗️</a><pre> <span class='Value'>q</span><span class='Ligature'>‿</span><span class='Value'>r</span> <span class='Gets'>↩</span> <span class='Brace'>{</span><span class='Value'>q</span><span class='Gets'>⇐</span><span class='Number'>2</span><span class='Function'>+</span><span class='Value'>r</span><span class='Gets'>⇐</span><span class='Number'>0.5</span><span class='Brace'>}</span> <span class='Separator'>⋄</span> <span class='Value'>q</span> 2.5 </pre> <p>With destructuring, you might want to discard some values from the right hand side rather than assign them any name. There's special syntax for this: use Nothing (<code><span class='Nothing'>·</span></code>) for a placeholder non-name in the appropriate position, like <code><span class='Nothing'>·</span><span class='Ligature'>‿</span><span class='Value'>y</span><span class='Ligature'>‿</span><span class='Nothing'>·</span> <span class='Gets'>←</span> <span class='Value'>list</span></code>.</p> diff --git a/docs/doc/find.html b/docs/doc/find.html index 01efff6d..7d30ff18 100644 --- a/docs/doc/find.html +++ b/docs/doc/find.html @@ -67,7 +67,7 @@ ┘ </pre> <p>It's also allowed for <code><span class='Value'>𝕨</span></code> to have a smaller rank than <code><span class='Value'>𝕩</span></code>; the axes of <code><span class='Value'>𝕨</span></code> then correspond to trailing axes of <code><span class='Value'>𝕩</span></code>, so that leading axes of <code><span class='Value'>𝕩</span></code> are mapped over. This is a minor violation of the <a href="leading.html">leading axis</a> principle, which would match axes of <code><span class='Value'>𝕨</span></code> to leading axes of <code><span class='Value'>𝕩</span></code> in order to make a function that's useful with the Rank operator, but such a function would be quite strange and hardly ever useful.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MOKAvzHigL8w4oC/MSDijbcgYQ==">↗️</a><pre> <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='Function'>⍷</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIDcgKDR84ouGy5wp4oyc4peL4oaVIDkgICAjIEFycmF5IHdpdGggcGF0dGVybnMKMOKAvzHigL8w4oC/MSDijbcgYQ==">↗️</a><pre> <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='Function'>⍷</span> <span class='Value'>a</span> ┌─ ╵ 0 0 0 0 0 0 0 0 0 0 0 0 diff --git a/docs/doc/fold.html b/docs/doc/fold.html index 5b024ff4..d59caf97 100644 --- a/docs/doc/fold.html +++ b/docs/doc/fold.html @@ -195,7 +195,7 @@ ⟨ 9 7 12 ⟩ </pre> <p>The Insert (<code><span class='Modifier'>˝</span></code>) modifier will do this for you. And because it works on the <a href="leading.html">leading axis</a> of the argument, Insert can be applied to axes other than the first with Rank. Sum each row (second axis) with <a href="rank.html#cells">Cells</a> (<code><span class='Modifier'>˘</span></code>), for example.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8udy5ggdGFi">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>˝˘</span> <span class='Value'>tab</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHRhYiDihpAgKDIr4oaVNSkgfOKMnCA5K+KGlTMKK8udy5ggdGFi">↗️</a><pre> <span class='Function'>+</span><span class='Modifier'>˝˘</span> <span class='Value'>tab</span> ⟨ 2 3 6 5 12 ⟩ </pre> <p>This case is tricky, because <code><span class='Function'>+</span><span class='Modifier'>´˘</span> <span class='Value'>tab</span></code> yields the same result but is actually unsound—if <code><span class='Value'>tab</span></code> contains arrays then they will be merged together at the end. Remember: if you want to reduce along one axis of an array and get an array of results out, you should use Insert (possibly adding Each to work on elements instead of cells; see <a href="#apl2-reduction">APL2 reduction</a> below).</p> @@ -243,7 +243,7 @@ <p>As a historical note, Insert is named after J's adverb <code><span class='Function'>/</span></code>, which comes from SHARP APL's <code><span class='Value'>⌿</span></code>, reduce-down. In the original APL, only arithmetic reductions were defined, and nested arrays didn't exist—arrays were either all characters or all numbers. SHARP extended them by splitting the array into cells as we've shown. However, there's another interpretation, which is what you'll find in mainstream APLs today…</p> <h2 id="apl2-reduction"><a class="header" href="#apl2-reduction">APL2 reduction?</a></h2> <p>The function <code><span class='Value'>⍪⌿</span></code> in Dyalog APL gives very different results from BQN's <code><span class='Function'>∾</span><span class='Modifier'>˝</span></code>. Instead of combining the cells like we see above, APL applies the function on pairs of <em>elements</em> much like Fold. The difference is that, because reduction happens only along one axis but an array might have other axes, there can be multiple values in the result, so that it will always be an array like the argument. BQN can perform this operation as well: <code><span class='Value'>⍪⌿</span></code> is written <code><span class='Function'>∾</span><span class='Modifier'>¨˝</span></code> in BQN (but please use <code><span class='Function'><</span><span class='Modifier'>˘</span><span class='Function'>⍉</span></code> instead).</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dGFiCgriiL7CqMudIHRhYg==">↗️</a><pre> <span class='Value'>tab</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHRhYiDihpAgKDIr4oaVNSkgfOKMnCA5K+KGlTMKdGFiCgriiL7CqMudIHRhYg==">↗️</a><pre> <span class='Value'>tab</span> ┌─ ╵ 1 0 1 0 1 2 diff --git a/docs/doc/functional.html b/docs/doc/functional.html index f3eea496..bec89fee 100644 --- a/docs/doc/functional.html +++ b/docs/doc/functional.html @@ -76,31 +76,31 @@ <span class='Brace'>}</span> </pre> <p>We can pass it the <a href="arithmetic.html#basic-arithmetic">exponential</a> function as an argument by giving it the name <code><span class='Function'>Exp</span></code> and then referring to it in lowercase (that is, in a subject role). The result is a <a href="train.html">train</a> that adds 1 to <em>e</em>-1 times the argument (we'll discuss only tacit functions here; for blocks see <a href="lexical.html">lexical scoping</a>).</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RXhwIOKGkCDii4YKTGluIGV4cA==">↗️</a><pre> <span class='Function'>Exp</span> <span class='Gets'>←</span> <span class='Function'>⋆</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=TGluIOKGkCB7CiAgdjAg4oaQIPCdlY8gMAogIHYwICsgKCjwnZWPIDEpIC0gdjApIMOXIOKKogp9CkV4cCDihpAg4ouGCkxpbiBleHA=">↗️</a><pre> <span class='Function'>Exp</span> <span class='Gets'>←</span> <span class='Function'>⋆</span> <span class='Function'>Lin</span> <span class='Value'>exp</span> 1+1.718281828459045×⊢ </pre> <p>As with all functions, the result of <code><span class='Function'>Lin</span></code> has a subject role. To use it as a function, we give it a name and then use that name with an uppercase spelling.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ZXhwTGluIOKGkCBMaW4gZXhwCkV4cExpbiA1">↗️</a><pre> <span class='Value'>expLin</span> <span class='Gets'>←</span> <span class='Function'>Lin</span> <span class='Value'>exp</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=TGluIOKGkCB7CiAgdjAg4oaQIPCdlY8gMAogIHYwICsgKCjwnZWPIDEpIC0gdjApIMOXIOKKogp9CkV4cCDihpAg4ouGCmV4cExpbiDihpAgTGluIGV4cApFeHBMaW4gNQ==">↗️</a><pre> <span class='Value'>expLin</span> <span class='Gets'>←</span> <span class='Function'>Lin</span> <span class='Value'>exp</span> <span class='Function'>ExpLin</span> <span class='Number'>5</span> 9.591409142295225 </pre> <p>A tricker but more compact method is to use the 1-modifier <code><span class='Brace'>{</span><span class='Function'>𝔽</span><span class='Brace'>}</span></code>, as a modifier's operand can have a subject or function role but its output always has a function role.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KExpbiBleHApe/CdlL19IDU=">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>Lin</span> <span class='Value'>exp</span><span class='Paren'>)</span><span class='Brace'>{</span><span class='Function'>𝔽</span><span class='Brace'>}</span> <span class='Number'>5</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=TGluIOKGkCB7CiAgdjAg4oaQIPCdlY8gMAogIHYwICsgKCjwnZWPIDEpIC0gdjApIMOXIOKKogp9CkV4cCDihpAg4ouGCihMaW4gZXhwKXvwnZS9fSA1">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>Lin</span> <span class='Value'>exp</span><span class='Paren'>)</span><span class='Brace'>{</span><span class='Function'>𝔽</span><span class='Brace'>}</span> <span class='Number'>5</span> 9.591409142295225 </pre> <p>Not the most accurate approximation, though.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RXhwIDU=">↗️</a><pre> <span class='Function'>Exp</span> <span class='Number'>5</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RXhwIOKGkCDii4YKRXhwIDU=">↗️</a><pre> <span class='Function'>Exp</span> <span class='Number'>5</span> 148.4131591025766 </pre> <p>Note also in this case that we could have used a modifier with a very similar definition to <code><span class='Function'>Lin</span></code>. The modifier is identical in definition except that <code><span class='Function'>𝕏</span></code> is replaced with <code><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=X2xpbiDihqkgewogIHYwIOKGkCDwnZS9IDAKICB2MCArICgo8J2UvSAxKSAtIHYwKSDDlyDiiqIKfQ==">↗️</a><pre> <span class='Modifier'>_lin</span> <span class='Gets'>↩</span> <span class='Brace'>{</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=TGluIOKGkCB7CiAgdjAg4oaQIPCdlY8gMAogIHYwICsgKCjwnZWPIDEpIC0gdjApIMOXIOKKogp9Cl9saW4g4oapIHsKICB2MCDihpAg8J2UvSAwCiAgdjAgKyAoKPCdlL0gMSkgLSB2MCkgw5cg4oqiCn0=">↗️</a><pre> <span class='Modifier'>_lin</span> <span class='Gets'>↩</span> <span class='Brace'>{</span> <span class='Value'>v0</span> <span class='Gets'>←</span> <span class='Function'>𝔽</span> <span class='Number'>0</span> <span class='Value'>v0</span> <span class='Function'>+</span> <span class='Paren'>((</span><span class='Function'>𝔽</span> <span class='Number'>1</span><span class='Paren'>)</span> <span class='Function'>-</span> <span class='Value'>v0</span><span class='Paren'>)</span> <span class='Function'>×</span> <span class='Function'>⊢</span> <span class='Brace'>}</span> </pre> <p>Its call syntax is simpler as well. In other cases, however, the function version might be preferable, for example when dealing with arrays of functions or many arguments including a function.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RXhwIF9saW4gNQ==">↗️</a><pre> <span class='Function'>Exp</span> <span class='Modifier'>_lin</span> <span class='Number'>5</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=TGluIOKGkCB7CiAgdjAg4oaQIPCdlY8gMAogIHYwICsgKCjwnZWPIDEpIC0gdjApIMOXIOKKogp9CkV4cCDihpAg4ouGCl9saW4g4oapIHsKICB2MCDihpAg8J2UvSAwCiAgdjAgKyAoKPCdlL0gMSkgLSB2MCkgw5cg4oqiCn0KRXhwIF9saW4gNQ==">↗️</a><pre> <span class='Function'>Exp</span> <span class='Modifier'>_lin</span> <span class='Number'>5</span> 9.591409142295225 </pre> <h3 id="arrays-of-functions"><a class="header" href="#arrays-of-functions">Arrays of functions</a></h3> diff --git a/docs/doc/group.html b/docs/doc/group.html index d9b16bb2..b2227519 100644 --- a/docs/doc/group.html +++ b/docs/doc/group.html @@ -76,7 +76,7 @@ </pre> <p>(Could we define <code><span class='Value'>phrase</span></code> more easily? See <a href="#partitioning">below</a>.)</p> <p>If we'd like to ignore words of 0 letters, or more than 5, we can set all word lengths greater than 5 to 0, then reduce the lengths by 1. Two words end up with left argument values of ¯1 and are omitted from the result.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MSAty5wg4omk4p+cNeKKuMOXIOKJoMKoIHBocmFzZQoK4omNy5ggezEty5ziiaTin5w14oq4w5fiiaDCqPCdlal94oq44oqUIHBocmFzZQ==">↗️</a><pre> <span class='Number'>1</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='Function'>≤</span><span class='Modifier2'>⟜</span><span class='Number'>5</span><span class='Modifier2'>⊸</span><span class='Function'>×</span> <span class='Function'>≠</span><span class='Modifier'>¨</span> <span class='Value'>phrase</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cGhyYXNlIOKGkCAiQlFOIuKAvyJ1c2VzIuKAvyJub3RhdGlvbiLigL8iYXMi4oC/ImEi4oC/InRvb2wi4oC/Im9mIuKAvyJ0aG91Z2h0IgoxIC3LnCDiiaTin5w14oq4w5cg4omgwqggcGhyYXNlCgriiY3LmCB7MS3LnOKJpOKfnDXiirjDl+KJoMKo8J2VqX3iirjiipQgcGhyYXNl">↗️</a><pre> <span class='Number'>1</span> <span class='Function'>-</span><span class='Modifier'>˜</span> <span class='Function'>≤</span><span class='Modifier2'>⟜</span><span class='Number'>5</span><span class='Modifier2'>⊸</span><span class='Function'>×</span> <span class='Function'>≠</span><span class='Modifier'>¨</span> <span class='Value'>phrase</span> ⟨ 2 3 ¯1 1 0 3 1 ¯1 ⟩ <span class='Function'>≍</span><span class='Modifier'>˘</span> <span class='Brace'>{</span><span class='Number'>1</span><span class='Function'>-</span><span class='Modifier'>˜</span><span class='Function'>≤</span><span class='Modifier2'>⟜</span><span class='Number'>5</span><span class='Modifier2'>⊸</span><span class='Function'>×≠</span><span class='Modifier'>¨</span><span class='Value'>𝕩</span><span class='Brace'>}</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span> <span class='Value'>phrase</span> @@ -88,7 +88,7 @@ ┘ </pre> <p>Note that the length of the result is determined by the largest index. So the result never includes trailing empty groups. A reader of the above code might expect 5 groups (lengths 1 through 5), but there are no words of length 5, so the last group isn't there. To ensure the result always has 5 groups, we can add a <code><span class='Number'>5</span></code> at the end of the left argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omNy5ggezXiiL7LnDEty5ziiaTin5w14oq4w5fiiaDCqPCdlal94oq44oqUIHBocmFzZQ==">↗️</a><pre> <span class='Function'>≍</span><span class='Modifier'>˘</span> <span class='Brace'>{</span><span class='Number'>5</span><span class='Function'>∾</span><span class='Modifier'>˜</span><span class='Number'>1</span><span class='Function'>-</span><span class='Modifier'>˜</span><span class='Function'>≤</span><span class='Modifier2'>⟜</span><span class='Number'>5</span><span class='Modifier2'>⊸</span><span class='Function'>×≠</span><span class='Modifier'>¨</span><span class='Value'>𝕩</span><span class='Brace'>}</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span> <span class='Value'>phrase</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cGhyYXNlIOKGkCAiQlFOIuKAvyJ1c2VzIuKAvyJub3RhdGlvbiLigL8iYXMi4oC/ImEi4oC/InRvb2wi4oC/Im9mIuKAvyJ0aG91Z2h0IgriiY3LmCB7NeKIvsucMS3LnOKJpOKfnDXiirjDl+KJoMKo8J2VqX3iirjiipQgcGhyYXNl">↗️</a><pre> <span class='Function'>≍</span><span class='Modifier'>˘</span> <span class='Brace'>{</span><span class='Number'>5</span><span class='Function'>∾</span><span class='Modifier'>˜</span><span class='Number'>1</span><span class='Function'>-</span><span class='Modifier'>˜</span><span class='Function'>≤</span><span class='Modifier2'>⟜</span><span class='Number'>5</span><span class='Modifier2'>⊸</span><span class='Function'>×≠</span><span class='Modifier'>¨</span><span class='Value'>𝕩</span><span class='Brace'>}</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span> <span class='Value'>phrase</span> ┌─ ╵ ⟨ "a" ⟩ ⟨ "as" "of" ⟩ @@ -137,7 +137,7 @@ ┘ </pre> <p>If we would like a particular index to key correspondence, we can use a fixed left argument to <a href="search.html#index-of">Index Of</a>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Y291bnRyaWVzIOKGkCAiSVQi4oC/IkpQIuKAvyJOTyLigL8iU1Ui4oC/IlVTIgpjb3VudHJpZXMg4omNy5ggY28gY291bnRyaWVz4oq44oqQ4oq44oqUIGxu">↗️</a><pre> <span class='Value'>countries</span> <span class='Gets'>←</span> <span class='String'>"IT"</span><span class='Ligature'>‿</span><span class='String'>"JP"</span><span class='Ligature'>‿</span><span class='String'>"NO"</span><span class='Ligature'>‿</span><span class='String'>"SU"</span><span class='Ligature'>‿</span><span class='String'>"US"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=bG4g4oaQICJQaGVscHMi4oC/IkxhdHluaW5hIuKAvyJCasO4cmdlbiLigL8iQW5kcmlhbm92IuKAvyJCasO4cm5kYWxlbiIKY28g4oaQICJVUyIgICAg4oC/IlNVIiAgICAgIOKAvyJOTyIgICAgIOKAvyJTVSIgICAgICAg4oC/Ik5PIgpjb3VudHJpZXMg4oaQICJJVCLigL8iSlAi4oC/Ik5PIuKAvyJTVSLigL8iVVMiCmNvdW50cmllcyDiiY3LmCBjbyBjb3VudHJpZXPiirjiipDiirjiipQgbG4=">↗️</a><pre> <span class='Value'>countries</span> <span class='Gets'>←</span> <span class='String'>"IT"</span><span class='Ligature'>‿</span><span class='String'>"JP"</span><span class='Ligature'>‿</span><span class='String'>"NO"</span><span class='Ligature'>‿</span><span class='String'>"SU"</span><span class='Ligature'>‿</span><span class='String'>"US"</span> <span class='Value'>countries</span> <span class='Function'>≍</span><span class='Modifier'>˘</span> <span class='Value'>co</span> <span class='Value'>countries</span><span class='Modifier2'>⊸</span><span class='Function'>⊐</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span> <span class='Value'>ln</span> ┌─ ╵ "IT" ⟨⟩ @@ -148,7 +148,7 @@ ┘ </pre> <p>However, this solution will fail if there are trailing keys with no values. To force the result to have a particular length you can append that length to the left argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Y291bnRyaWVzIOKGqSAiSVQi4oC/IkpQIuKAvyJOTyLigL8iU1Ui4oC/IlVTIuKAvyJaVyIKY291bnRyaWVzIOKJjcuYIGNvIGNvdW50cmllc+KKuCjiipDiiL7iiaDiiJjiiqMp4oq44oqUIGxu">↗️</a><pre> <span class='Value'>countries</span> <span class='Gets'>↩</span> <span class='String'>"IT"</span><span class='Ligature'>‿</span><span class='String'>"JP"</span><span class='Ligature'>‿</span><span class='String'>"NO"</span><span class='Ligature'>‿</span><span class='String'>"SU"</span><span class='Ligature'>‿</span><span class='String'>"US"</span><span class='Ligature'>‿</span><span class='String'>"ZW"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=bG4g4oaQICJQaGVscHMi4oC/IkxhdHluaW5hIuKAvyJCasO4cmdlbiLigL8iQW5kcmlhbm92IuKAvyJCasO4cm5kYWxlbiIKY28g4oaQICJVUyIgICAg4oC/IlNVIiAgICAgIOKAvyJOTyIgICAgIOKAvyJTVSIgICAgICAg4oC/Ik5PIgpjb3VudHJpZXMg4oaQICJJVCLigL8iSlAi4oC/Ik5PIuKAvyJTVSLigL8iVVMiCmNvdW50cmllcyDihqkgIklUIuKAvyJKUCLigL8iTk8i4oC/IlNVIuKAvyJVUyLigL8iWlciCmNvdW50cmllcyDiiY3LmCBjbyBjb3VudHJpZXPiirgo4oqQ4oi+4omg4oiY4oqjKeKKuOKKlCBsbg==">↗️</a><pre> <span class='Value'>countries</span> <span class='Gets'>↩</span> <span class='String'>"IT"</span><span class='Ligature'>‿</span><span class='String'>"JP"</span><span class='Ligature'>‿</span><span class='String'>"NO"</span><span class='Ligature'>‿</span><span class='String'>"SU"</span><span class='Ligature'>‿</span><span class='String'>"US"</span><span class='Ligature'>‿</span><span class='String'>"ZW"</span> <span class='Value'>countries</span> <span class='Function'>≍</span><span class='Modifier'>˘</span> <span class='Value'>co</span> <span class='Value'>countries</span><span class='Modifier2'>⊸</span><span class='Paren'>(</span><span class='Function'>⊐∾≠</span><span class='Modifier2'>∘</span><span class='Function'>⊣</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>⊔</span> <span class='Value'>ln</span> ┌─ ╵ "IT" ⟨⟩ diff --git a/docs/doc/join.html b/docs/doc/join.html index e2876a74..b0f9ead4 100644 --- a/docs/doc/join.html +++ b/docs/doc/join.html @@ -33,11 +33,11 @@ ┘ </pre> <p>For this definition to work, major cells of <code><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕩</span></code> have to have the same shape. That means that <code><span class='Value'>𝕨</span><span class='Function'>≡</span><span class='Modifier2'>○</span><span class='Paren'>(</span><span class='Number'>1</span><span class='Function'>↓≢</span><span class='Paren'>)</span><span class='Value'>𝕩</span></code>, and the shape of the result is the sum of the lengths of <code><span class='Value'>𝕨</span></code> and <code><span class='Value'>𝕩</span></code> followed by their shared major cell shape: to use a self-referential definition, the final shape is given by <code><span class='Function'>+</span><span class='Modifier2'>○</span><span class='Function'>≠</span> <span class='Function'>∾</span> <span class='Function'>⊣</span><span class='Modifier'>⁼</span><span class='Modifier2'>○</span><span class='Paren'>(</span><span class='Number'>1</span><span class='Function'>↓≢</span><span class='Paren'>)</span></code> for arguments of equal rank.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDiiL4gMuKAvzXipYpiICAjIFNoYXBlcyBkb24ndCBmaXQ=">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>∾</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Function'>⥊</span><span class='Value'>b</span> <span class='Comment'># Shapes don't fit +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIDMgK+KMnOKXi+KGlSA0CuKKoiBiIOKGkCAy4oC/NCDipYog4oaVOAphIOKIviAy4oC/NeKlimIgICMgU2hhcGVzIGRvbid0IGZpdA==">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>∾</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>5</span><span class='Function'>⥊</span><span class='Value'>b</span> <span class='Comment'># Shapes don't fit </span><span class='Error'>Error: ∾: Lengths not matchable (3‿4 ≡ ≢𝕨, 2‿5 ≡ ≢𝕩)</span> </pre> <p>Join To will also allow arguments with ranks that are one apart. In this case, the smaller-rank argument is treated as a major cell in its entirety. If for example <code><span class='Value'>𝕨</span><span class='Function'><</span><span class='Modifier2'>○</span><span class='Function'>=</span><span class='Value'>𝕩</span></code>, then we must have <code><span class='Paren'>(</span><span class='Function'>≢</span><span class='Value'>𝕨</span><span class='Paren'>)</span><span class='Function'>≡</span><span class='Number'>1</span><span class='Function'>↓≢</span><span class='Value'>𝕩</span></code>, and the result shape is <code><span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>+</span><span class='Modifier2'>⌾</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=NOKAvzLigL8z4oC/MCDiiL4gYQ==">↗️</a><pre> <span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>∾</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIDMgK+KMnOKXi+KGlSA0CjTigL8y4oC/M+KAvzAg4oi+IGE=">↗️</a><pre> <span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>∾</span> <span class='Value'>a</span> ┌─ ╵ 4 2 3 0 0 1 2 3 diff --git a/docs/doc/leading.html b/docs/doc/leading.html index b3b008c8..63cdc85f 100644 --- a/docs/doc/leading.html +++ b/docs/doc/leading.html @@ -34,7 +34,7 @@ ┘ </pre> <p>To use these functions on another axis, use the <a href="rank.html#rank">Rank</a> (<code><span class='Modifier2'>⎉</span></code>) or <a href="rank.html#cells">Cells</a> (<code><span class='Modifier'>˘</span></code>) modifier to find the one you want. For a rank 2 array like <code><span class='Value'>a</span></code>, the most you'll ever need is a single <code><span class='Modifier'>˘</span></code>, because after the leading one there's only one other axis.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqPy5ggYSAgICAgICAgICAgICAgICAgICMgRmlyc3QgY29sdW1uCgrijL3LmCBhICAgICAgICAgICAgICAgICAgIyBTd2FwIHRoZSBjb2x1bW5zCgriiqNgy5ggYSAgICAgICAgICAgICAgICAgIyBSZXBsaWNhdGUgYWxvbmcgcm93cw==">↗️</a><pre> <span class='Function'>⊏</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Comment'># First column +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIDPigL8yIOKliiAiYWJjZGVmIiAgIyBBbiBhcnJheSB3aXRoIHRocmVlIG1ham9yIGNlbGxzCuKKj8uYIGEgICAgICAgICAgICAgICAgICAjIEZpcnN0IGNvbHVtbgoK4oy9y5ggYSAgICAgICAgICAgICAgICAgICMgU3dhcCB0aGUgY29sdW1ucwoK4oqjYMuYIGEgICAgICAgICAgICAgICAgICMgUmVwbGljYXRlIGFsb25nIHJvd3M=">↗️</a><pre> <span class='Function'>⊏</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Comment'># First column </span>"ace" <span class='Function'>⌽</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Comment'># Swap the columns @@ -52,7 +52,7 @@ ┘ </pre> <p>In these three cases above, the results are the same as you would get from <a href="transpose.html">transposing</a> before and after (which does nothing to the rank-1 result of <code><span class='Function'>⊏</span><span class='Modifier'>˘</span></code>, but that's what's wanted). But in the following cases, the structure is quite different: <code><span class='Function'>↑</span><span class='Value'>a</span></code> is a list of matrices while <code><span class='Function'>↑</span><span class='Modifier'>˘</span><span class='Value'>a</span></code> is a matrix of lists. This is because the functions <code><span class='Function'>⊏</span></code>, <code><span class='Function'>⌽</span></code>, and <code><span class='Function'>⊣</span><span class='Modifier'>`</span></code> leave the trailing axis structure intact (<code><span class='Function'>⊏</span></code> removes one axis); taking into account that Rank or Cells always preserves the leading or frame axes, all axes are preserved (except the one removed by <code><span class='Function'>⊏</span></code>). But Prefixes or Suffixes move axes after the first from the whole of <code><span class='Value'>𝕩</span></code> to elements of the result, pushing them down in depth, and Rank won't undo this sort of structural change.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oaRIGEgICAgICAgICAgICAgICAgICAgIyBQcmVmaXhlcyBvZiBhOiAgICByYW5rcyAxfDIKCuKGkcuYIGEgICAgICAgICAgICAgICAgICAjIFByZWZpeGVzIG9mIHJvd3M6IHJhbmtzIDJ8MQoK4oi+y50gYSAgICAgICAgICAgICAgICAgICMgSm9pbiB0aGUgY2VsbHMKCuKIvsudy5ggYSAgICAgICAgICAgICAgICAgIyBKb2luLWluc2VydCBpcyBhIG5vLW9wIG9uIGxpc3Rz">↗️</a><pre> <span class='Function'>↑</span> <span class='Value'>a</span> <span class='Comment'># Prefixes of a: ranks 1|2 +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIDPigL8yIOKliiAiYWJjZGVmIiAgIyBBbiBhcnJheSB3aXRoIHRocmVlIG1ham9yIGNlbGxzCuKGkSBhICAgICAgICAgICAgICAgICAgICMgUHJlZml4ZXMgb2YgYTogICAgcmFua3MgMXwyCgrihpHLmCBhICAgICAgICAgICAgICAgICAgIyBQcmVmaXhlcyBvZiByb3dzOiByYW5rcyAyfDEKCuKIvsudIGEgICAgICAgICAgICAgICAgICAjIEpvaW4gdGhlIGNlbGxzCgriiL7LncuYIGEgICAgICAgICAgICAgICAgICMgSm9pbi1pbnNlcnQgaXMgYSBuby1vcCBvbiBsaXN0cw==">↗️</a><pre> <span class='Function'>↑</span> <span class='Value'>a</span> <span class='Comment'># Prefixes of a: ranks 1|2 </span>┌─ · ↕0‿2 ┌─ ┌─ ┌─ ╵"ab" ╵"ab ╵"ab @@ -79,7 +79,7 @@ ┘ </pre> <p><a href="couple.html">Solo</a> (<code><span class='Function'>≍</span></code>), something of a maverick, manages to act on <em>zero</em> leading axes of <code><span class='Value'>𝕩</span></code> by creating the first axis of the <em>result</em> instead. Because it doesn't need any axis to work, it can go in front of either axis but also past the last one by working with rank 0, a case where most array functions would give an error.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKJjSBhICAgICAgICAgICAgICAgICAjIFNvbG8gYWRkcyBhIGxlbmd0aC0xIGF4aXMKCmEg4omhIOKKjyDiiY0gYSAgICAgICAgICAgICAjIEZpcnN0IENlbGwgdW5kb2VzIHRoaXMKCuKJoiDiiY3LmCBhICAgICAgICAgICAgICAgICMgU29sbyBjYW4gaW5zZXJ0IHRoZSBheGlzIGRlZXBlcuKApgoK4omiIOKJjeKOiTAgYSAgICAgICAgICAgICAgICMg4oCmb3IgZGVlcGVyIHN0aWxsLg==">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>≍</span> <span class='Value'>a</span> <span class='Comment'># Solo adds a length-1 axis +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIDPigL8yIOKliiAiYWJjZGVmIiAgIyBBbiBhcnJheSB3aXRoIHRocmVlIG1ham9yIGNlbGxzCuKJoiDiiY0gYSAgICAgICAgICAgICAgICAgIyBTb2xvIGFkZHMgYSBsZW5ndGgtMSBheGlzCgphIOKJoSDiio8g4omNIGEgICAgICAgICAgICAgIyBGaXJzdCBDZWxsIHVuZG9lcyB0aGlzCgriiaIg4omNy5ggYSAgICAgICAgICAgICAgICAjIFNvbG8gY2FuIGluc2VydCB0aGUgYXhpcyBkZWVwZXLigKYKCuKJoiDiiY3ijokwIGEgICAgICAgICAgICAgICAjIOKApm9yIGRlZXBlciBzdGlsbC4=">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>≍</span> <span class='Value'>a</span> <span class='Comment'># Solo adds a length-1 axis </span>⟨ 1 3 2 ⟩ <span class='Value'>a</span> <span class='Function'>≡</span> <span class='Function'>⊏</span> <span class='Function'>≍</span> <span class='Value'>a</span> <span class='Comment'># First Cell undoes this @@ -187,7 +187,7 @@ ┘ </pre> <p>That's shape <code><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span></code> matched with shape <code><span class='Bracket'>⟨</span><span class='Number'>3</span><span class='Bracket'>⟩</span></code>: the leading <code><span class='Number'>3</span></code> agrees. Now to match with <code><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span></code>:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGMg4oaQIDEwMCDDlyAzID3ijJzil4vihpUgMiAgIyBBIHJhbmstMiBhcnJheSB0byBhZGQKCmMgKyB4ICAgICAgICAgICAgICAgICAjIDAtY2VsbHMgcGFpcmVkIHdpdGggMS1jZWxscw==">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>c</span> <span class='Gets'>←</span> <span class='Number'>100</span> <span class='Function'>×</span> <span class='Number'>3</span> <span class='Function'>=</span><span class='Modifier'>⌜</span><span class='Modifier2'>○</span><span class='Function'>↕</span> <span class='Number'>2</span> <span class='Comment'># A rank-2 array to add +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHgg4oaQIDPigL8y4oC/NCDipYog4oaVNjAgICAgICMgQSByYW5rLTMgYXJyYXkK4oqiIGMg4oaQIDEwMCDDlyAzID3ijJzil4vihpUgMiAgIyBBIHJhbmstMiBhcnJheSB0byBhZGQKCmMgKyB4ICAgICAgICAgICAgICAgICAjIDAtY2VsbHMgcGFpcmVkIHdpdGggMS1jZWxscw==">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>c</span> <span class='Gets'>←</span> <span class='Number'>100</span> <span class='Function'>×</span> <span class='Number'>3</span> <span class='Function'>=</span><span class='Modifier'>⌜</span><span class='Modifier2'>○</span><span class='Function'>↕</span> <span class='Number'>2</span> <span class='Comment'># A rank-2 array to add </span>┌─ ╵ 100 0 0 100 @@ -207,7 +207,7 @@ ┘ </pre> <p>And of course, identical shapes agree:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eCArIHggICAgICAgICAgICAgICAgICMgUGFpcndpc2UgYWRkaXRpb24=">↗️</a><pre> <span class='Value'>x</span> <span class='Function'>+</span> <span class='Value'>x</span> <span class='Comment'># Pairwise addition +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHgg4oaQIDPigL8y4oC/NCDipYog4oaVNjAgICAgICMgQSByYW5rLTMgYXJyYXkKeCArIHggICAgICAgICAgICAgICAgICMgUGFpcndpc2UgYWRkaXRpb24=">↗️</a><pre> <span class='Value'>x</span> <span class='Function'>+</span> <span class='Value'>x</span> <span class='Comment'># Pairwise addition </span>┌─ ╎ 0 2 4 6 8 10 12 14 diff --git a/docs/doc/lexical.html b/docs/doc/lexical.html index 4d34cdae..6ff7f51e 100644 --- a/docs/doc/lexical.html +++ b/docs/doc/lexical.html @@ -51,7 +51,7 @@ 42 </pre> <p>So the <code><span class='Function'>Count</span></code> function above increments and prints a global <code><span class='Value'>counter</span></code> by a global amount <code><span class='Value'>inc</span></code>, which is visible in <code><span class='Function'>Count</span></code> because it's visible everywhere. Or, not quite… if a block defines its <em>own</em> copy of <code><span class='Value'>inc</span></code>, then it will lose access to the outer one. However, code that comes before that definition can still see the outer variable. So it can copy its value at the start of the block (this won't reflect later changes to the value. Don't shadow variables you want to use!).</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eyBpbmPihpAzIOKLhCBpbmMgfSAgIyBpbmMgaXMgc2hhZG93ZWQKCmluYyAgIyBCdXQgaXQncyBzdGlsbCBoZXJlIGluIHRoZSBvdXRlciBzY29wZQoKeyBh4oaQaW5jIOKLhCBpbmPihpAzIOKLhCBhIH0gICMgUmVhZCBiZWZvcmUgc2hhZG93aW5n">↗️</a><pre> <span class='Brace'>{</span> <span class='Value'>inc</span><span class='Gets'>←</span><span class='Number'>3</span> <span class='Separator'>⋄</span> <span class='Value'>inc</span> <span class='Brace'>}</span> <span class='Comment'># inc is shadowed +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNgppbmMg4oaQIDYKeyBpbmPihpAzIOKLhCBpbmMgfSAgIyBpbmMgaXMgc2hhZG93ZWQKCmluYyAgIyBCdXQgaXQncyBzdGlsbCBoZXJlIGluIHRoZSBvdXRlciBzY29wZQoKeyBh4oaQaW5jIOKLhCBpbmPihpAzIOKLhCBhIH0gICMgUmVhZCBiZWZvcmUgc2hhZG93aW5n">↗️</a><pre> <span class='Brace'>{</span> <span class='Value'>inc</span><span class='Gets'>←</span><span class='Number'>3</span> <span class='Separator'>⋄</span> <span class='Value'>inc</span> <span class='Brace'>}</span> <span class='Comment'># inc is shadowed </span>3 <span class='Value'>inc</span> <span class='Comment'># But it's still here in the outer scope @@ -61,11 +61,11 @@ </span>6 </pre> <p>Each scope can only define a given name once. Trying to shadow a name that's in the current scope and not a higher one gives an error at compilation.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eyBpbmPihpAzIOKLhCBpbmPihpA0IH0=">↗️</a><pre> <span class='Brace'>{</span> <span class='Value'>inc</span><span class='Gets'>←</span><span class='Number'>3</span> <span class='Separator'>⋄</span> <span class='Value'>inc</span><span class='Gets'>←</span><span class='Number'>4</span> <span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aW5jIOKGkCA2CnsgaW5j4oaQMyDii4QgaW5j4oaQNCB9">↗️</a><pre> <span class='Brace'>{</span> <span class='Value'>inc</span><span class='Gets'>←</span><span class='Number'>3</span> <span class='Separator'>⋄</span> <span class='Value'>inc</span><span class='Gets'>←</span><span class='Number'>4</span> <span class='Brace'>}</span> <span class='Error'>Error: Redefinition</span> </pre> <p>Let's go all in on shadowing and make a modifier that creates its own copies of <code><span class='Value'>counter</span></code> and <code><span class='Value'>inc</span></code>, returning a custom version of the <code><span class='Function'>Count</span></code> function above.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=X21ha2VDb3VudCDihpAgeyBjb3VudGVy4oC/aW5j4oaQ8J2VlyDii4QgeyBjb3VudGVyICvihqkg8J2VqSDDlyBpbmMgfSB9CgpDM183IOKGkCAz4oC/NyBfbWFrZUNvdW50ICAjIFN0YXJ0IGF0IDM7IGluYyBieSA3CgpDM183IDAKQzNfNyAxCkNvdW50IDAgICMgT2xkIGNvdW50ZXIgc3RheXMgdGhlIHNhbWU=">↗️</a><pre> <span class='Modifier'>_makeCount</span> <span class='Gets'>←</span> <span class='Brace'>{</span> <span class='Value'>counter</span><span class='Ligature'>‿</span><span class='Value'>inc</span><span class='Gets'>←</span><span class='Value'>𝕗</span> <span class='Separator'>⋄</span> <span class='Brace'>{</span> <span class='Value'>counter</span> <span class='Function'>+</span><span class='Gets'>↩</span> <span class='Value'>𝕩</span> <span class='Function'>×</span> <span class='Value'>inc</span> <span class='Brace'>}</span> <span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Y291bnRlciDihpAgMAppbmMg4oaQIDYKQ291bnQg4oaQIHsgY291bnRlciAr4oapIPCdlakgw5cgaW5jIH0KX21ha2VDb3VudCDihpAgeyBjb3VudGVy4oC/aW5j4oaQ8J2VlyDii4QgeyBjb3VudGVyICvihqkg8J2VqSDDlyBpbmMgfSB9CgpDM183IOKGkCAz4oC/NyBfbWFrZUNvdW50ICAjIFN0YXJ0IGF0IDM7IGluYyBieSA3CgpDM183IDAKQzNfNyAxCkNvdW50IDAgICMgT2xkIGNvdW50ZXIgc3RheXMgdGhlIHNhbWU=">↗️</a><pre> <span class='Modifier'>_makeCount</span> <span class='Gets'>←</span> <span class='Brace'>{</span> <span class='Value'>counter</span><span class='Ligature'>‿</span><span class='Value'>inc</span><span class='Gets'>←</span><span class='Value'>𝕗</span> <span class='Separator'>⋄</span> <span class='Brace'>{</span> <span class='Value'>counter</span> <span class='Function'>+</span><span class='Gets'>↩</span> <span class='Value'>𝕩</span> <span class='Function'>×</span> <span class='Value'>inc</span> <span class='Brace'>}</span> <span class='Brace'>}</span> <span class='Function'>C3_7</span> <span class='Gets'>←</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>7</span> <span class='Modifier'>_makeCount</span> <span class='Comment'># Start at 3; inc by 7 </span> @@ -90,7 +90,7 @@ <p>Why define things this way? It's easier to see if you imagine the variable used is also a function. It's normal for a function to call other functions defined at the top level, of course. And it would be pretty unpleasant for BQN to enforce a specific ordering for them. It would also make recursive functions impossible except by using <code><span class='Function'>𝕊</span></code>, and mutually recursive ones completely impossible. A simple rule that makes all these things just work smoothly seems much better than any alternative.</p> <h2 id="closures"><a class="header" href="#closures">Closures</a></h2> <p>Let's run <code><span class='Modifier'>_makeCount</span></code> from above a few more times.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QzRfMiDihpAgNOKAvzIgX21ha2VDb3VudCAgIyBTdGFydCBhdCA0OyBpbmNyZW1lbnQgYnkgMgpDMV80IOKGkCAx4oC/NCBfbWFrZUNvdW50ICAjICAgICAgICAgIDE7ICAgICAgICAgICAgICA0CgpDNF8yIDAKQzFfNCAwCkM0XzIgMTAKQzFfNCAxMApDNF8yIDAKQzNfNyAwICAjIFRoZSBmaXJzdCBvbmUncyBzdGlsbCBhcm91bmQgdG9v">↗️</a><pre> <span class='Function'>C4_2</span> <span class='Gets'>←</span> <span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Modifier'>_makeCount</span> <span class='Comment'># Start at 4; increment by 2 +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=X21ha2VDb3VudCDihpAgeyBjb3VudGVy4oC/aW5j4oaQ8J2VlyDii4QgeyBjb3VudGVyICvihqkg8J2VqSDDlyBpbmMgfSB9CkMzXzcg4oaQIDPigL83IF9tYWtlQ291bnQgICMgU3RhcnQgYXQgMzsgaW5jIGJ5IDcKQzRfMiDihpAgNOKAvzIgX21ha2VDb3VudCAgIyBTdGFydCBhdCA0OyBpbmNyZW1lbnQgYnkgMgpDMV80IOKGkCAx4oC/NCBfbWFrZUNvdW50ICAjICAgICAgICAgIDE7ICAgICAgICAgICAgICA0CgpDNF8yIDAKQzFfNCAwCkM0XzIgMTAKQzFfNCAxMApDNF8yIDAKQzNfNyAwICAjIFRoZSBmaXJzdCBvbmUncyBzdGlsbCBhcm91bmQgdG9v">↗️</a><pre> <span class='Function'>C4_2</span> <span class='Gets'>←</span> <span class='Number'>4</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Modifier'>_makeCount</span> <span class='Comment'># Start at 4; increment by 2 </span> <span class='Function'>C1_4</span> <span class='Gets'>←</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>4</span> <span class='Modifier'>_makeCount</span> <span class='Comment'># 1; 4 </span> <span class='Function'>C4_2</span> <span class='Number'>0</span> @@ -139,7 +139,7 @@ </span>30 </pre> <p>Only source code with access to a variable can modify it! This means that if none of the code in a variable's scope modifies it, then the variable is constant. That is, constant once it's defined: remember that it's still possible to get an error if the variable is accessed before being defined.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=eyB7IGEgfSDii4QgYeKGkDQgfQ==">↗️</a><pre> <span class='Brace'>{</span> <span class='Brace'>{</span> <span class='Value'>a</span> <span class='Brace'>}</span> <span class='Separator'>⋄</span> <span class='Value'>a</span><span class='Gets'>←</span><span class='Number'>4</span> <span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNgp7IHsgYSB9IOKLhCBh4oaQNCB9">↗️</a><pre> <span class='Brace'>{</span> <span class='Brace'>{</span> <span class='Value'>a</span> <span class='Brace'>}</span> <span class='Separator'>⋄</span> <span class='Value'>a</span><span class='Gets'>←</span><span class='Number'>4</span> <span class='Brace'>}</span> <span class='Error'>Error: Reading variable before its defined</span> </pre> <p>With lexical scoping, variable mutation automatically leads to mutable data. This is because a function or modifier that depends on the variable value changes its behavior when the variable changes. So do objects; this slightly more concrete case is discussed <a href="oop.html#mutability">here</a>. The behavior change is observed by calling operations, and by accessing object fields. These are the only two actions that might behave differently when applied to the same values!</p> diff --git a/docs/doc/match.html b/docs/doc/match.html index 767a4bb5..13568805 100644 --- a/docs/doc/match.html +++ b/docs/doc/match.html @@ -67,7 +67,7 @@ </span>13 </pre> <p>(A side note is that BQN restricts what can cause these side effects: they can only happen by calling a block function or modifier, and never a primitive or purely <a href="tacit.html">tacit</a> operation). Now suppose we share the value of <code><span class='Function'>F</span></code> with another variable like <code><span class='Function'>F1</span></code> below. When we apply <code><span class='Function'>G</span></code>, the result of <code><span class='Function'>F</span></code> might change, but so does <code><span class='Function'>F1</span></code>! This effect is called <a href="https://en.wikipedia.org/wiki/Aliasing_(computing)">aliasing</a>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RjEg4oaQIEYKe/CdlY8gNn3CqCBG4oC/RjEKCkcgMwp78J2VjyA2fcKoIEbigL9GMQ==">↗️</a><pre> <span class='Function'>F1</span> <span class='Gets'>←</span> <span class='Function'>F</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RuKAv0cg4oaQIHsgYeKGkDEwIOKLhCB7YSvwnZWpfeKAv3th4oap8J2VqX0gfQpGMSDihpAgRgp78J2VjyA2fcKoIEbigL9GMQoKRyAzCnvwnZWPIDZ9wqggRuKAv0Yx">↗️</a><pre> <span class='Function'>F1</span> <span class='Gets'>←</span> <span class='Function'>F</span> <span class='Brace'>{</span><span class='Function'>𝕏</span> <span class='Number'>6</span><span class='Brace'>}</span><span class='Modifier'>¨</span> <span class='Function'>F</span><span class='Ligature'>‿</span><span class='Function'>F1</span> ⟨ 14 14 ⟩ @@ -77,7 +77,7 @@ ⟨ 9 9 ⟩ </pre> <p>In some cases you might not be able to demonstrate aliasing so cleanly. A function such as a random number generator changes its own state, so calling one function will change the other. But comparison tells you directly whether two blocks are the same.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ZiA9IGYx">↗️</a><pre> <span class='Value'>f</span> <span class='Function'>=</span> <span class='Value'>f1</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RuKAv0cg4oaQIHsgYeKGkDEwIOKLhCB7YSvwnZWpfeKAv3th4oap8J2VqX0gfQpGMSDihpAgRgpmID0gZjE=">↗️</a><pre> <span class='Value'>f</span> <span class='Function'>=</span> <span class='Value'>f1</span> 1 </pre> <p>As with other kinds of functions, just because two blocks always behave the same doesn't mean they are equal. Any function that's written as <code><span class='Brace'>{</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code> will always work the same as other functions spelled that way, but the two functions below are different instances because they come from two different places in the source code.</p> @@ -92,7 +92,7 @@ ⟨ 8 12 ⟩ </pre> <p>These functions both have the definition <code><span class='Brace'>{</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code>, but give different results! They are different <em>instances</em> of the same block, and have different environments: for <code><span class='Function'>T2</span></code>, <code><span class='Value'>a</span></code> is <code><span class='Number'>2</span></code>, and for <code><span class='Function'>T3</span></code>, it's <code><span class='Number'>3</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dDIgPSB0Mw==">↗️</a><pre> <span class='Value'>t2</span> <span class='Function'>=</span> <span class='Value'>t3</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dDIg4oaQIEdlbiAyCnQzIOKGkCBHZW4gMwp0MiA9IHQz">↗️</a><pre> <span class='Value'>t2</span> <span class='Function'>=</span> <span class='Value'>t3</span> 0 </pre> <p>Some definitions should help to make things clearer. A "block" is not actually a BQN value, but a region of source code enclosed in <code><span class='Brace'>{}</span></code> brackets. When the program encounters a block function or modifier, it creates an instance of this block, and then uses this instance in the rest of the expression (actually, an immediate block also creates an instance, but this instance is immediately run, and discarded when it finishes, so it can't be accessed as a value). Every time the function <code><span class='Function'>Gen</span></code> is run, it evaluates the statements it contains, and the second statement <code><span class='Brace'>{</span><span class='Value'>a</span><span class='Function'>×</span><span class='Value'>𝕩</span><span class='Brace'>}</span></code> creates a block instance. So <code><span class='Function'>Gen</span></code> creates a new block instance each time. This is necessary for <code><span class='Function'>Gen</span></code> to work correctly: each time it runs, it creates a new scope, so it needs to create a new function that will be tied to that scope.</p> diff --git a/docs/doc/oop.html b/docs/doc/oop.html index 4102ddc1..093068f0 100644 --- a/docs/doc/oop.html +++ b/docs/doc/oop.html @@ -84,7 +84,7 @@ </pre> <p>Two variables <code><span class='Value'>l</span></code> and <code><span class='Function'>Transfer</span></code> aren't exported, for two different reasons. <code><span class='Value'>l</span></code> encodes the state of the tower, but it's only exposed with the function <code><span class='Function'>View</span></code>, which allows the internal representation to be changed freely. <code><span class='Function'>Transfer</span></code> is just a utility function. While it's not dangerous to use outside of the object, there's no reason to expose it through <code><span class='Value'>towerOfHanoi</span></code>'s interface. If it's wanted in another place it should be moved to a common location.</p> <p>Here are the results of a few applications of these functions.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dCDihpAgdG93ZXJPZkhhbm9pCnQuVmlld0AKCnQuTW92ZSAw4oC/MgoKdC5Nb3ZlIDHigL8yCgp0Lk1vdmUgMOKAvzEKCnQuTW92ZSAy4oC/MQ==">↗️</a><pre> <span class='Value'>t</span> <span class='Gets'>←</span> <span class='Value'>towerOfHanoi</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dG93ZXJPZkhhbm9pIOKGkCB7CiAgbCDihpAg4oaVwqg14oC/MOKAvzAKICBWaWV3IOKHkCB78J2VpAogICAgbAogIH0KICBNb3ZlIOKHkCB7ZnJvbeKAv3RvOgogICAgbCDihqkgVHJhbnNmZXLCtOKMvijwnZWp4oq44oqPKeKNnyjiiaDCtPCdlakpIGwKICB9CiAgIyBNb3ZlIGEgZGlzayBmcm9tIPCdlaggdG8g8J2VqQogIFRyYW5zZmVyIOKGkCB7CiAgICAiTm8gZGlzayB0byBtb3ZlIiEwPOKJoPCdlagKICAgICJDYW4ndCBwbGFjZSBsYXJnZXIgZGlzayBvbiBzbWFsbGVyIG9uZSIhKDA84omgKeKXtuKfqDEs8J2VqDzil4viipHiiqLin6nwnZWpCiAgICDin6gx4oaT8J2VqCwg8J2VqOKKj+KKuOKIvvCdlanin6kKICB9Cn0KdCDihpAgdG93ZXJPZkhhbm9pCnQuVmlld0AKCnQuTW92ZSAw4oC/MgoKdC5Nb3ZlIDHigL8yCgp0Lk1vdmUgMOKAvzEKCnQuTW92ZSAy4oC/MQ==">↗️</a><pre> <span class='Value'>t</span> <span class='Gets'>←</span> <span class='Value'>towerOfHanoi</span> <span class='Value'>t.</span><span class='Function'>View</span><span class='String'>@</span> ⟨ ⟨ 0 1 2 3 4 ⟩ ⟨⟩ ⟨⟩ ⟩ diff --git a/docs/doc/order.html b/docs/doc/order.html index 15111965..050b6763 100644 --- a/docs/doc/order.html +++ b/docs/doc/order.html @@ -79,7 +79,7 @@ ⟨ 3 1 0 2 ⟩ </pre> <p>Given our list <code><span class='Value'>l</span></code> of things in a solar system, Sort Up orders them by size, or maybe alphabetically. What does <code><span class='Function'>⍋</span><span class='Value'>l</span></code> do? Its result also orders these items, but instead of listing them directly, each element is the <em>index</em> of that cell in the argument. So the way to read it is that the first item in sorted order is cell <code><span class='Number'>3</span></code> of the argument, <code><span class='String'>"asteroid"</span></code>. The second is cell <code><span class='Number'>1</span></code>, <code><span class='String'>"moon"</span></code>, and the third—forget this, we made programming languages for a reason.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKNi2wpIOKKjyBs">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>⍋</span><span class='Value'>l</span><span class='Paren'>)</span> <span class='Function'>⊏</span> <span class='Value'>l</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGwg4oaQICJwbGFuZXQi4oC/Im1vb24i4oC/InN0YXIi4oC/ImFzdGVyb2lkIgoo4o2LbCkg4oqPIGw=">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>⍋</span><span class='Value'>l</span><span class='Paren'>)</span> <span class='Function'>⊏</span> <span class='Value'>l</span> ⟨ "asteroid" "moon" "planet" "star" ⟩ </pre> <h3 id="ordinals"><a class="header" href="#ordinals">Ordinals</a></h3> @@ -145,7 +145,7 @@ </svg> <p>So the elements of the Grade of an array correspond to the cells of that array after it's sorted. It's tempting if you don't have the sorted list handy to try to match them up with major cells of the original array, but this never makes sense—there's no relationship. However, applying Grade <em>twice</em> gives us a list that does correspond to the original argument quite usefully: it says, for each major cell of that argument, what rank it has relative to the others (smallest is 0, next is 1, and so on, breaking ties in favor of which cell comes earlier in the argument). Experienced APL programmers call this pattern the "ordinals" idiom.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=bCDiiY0g4o2L4o2LIGw=">↗️</a><pre> <span class='Value'>l</span> <span class='Function'>≍</span> <span class='Function'>⍋⍋</span> <span class='Value'>l</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGwg4oaQICJwbGFuZXQi4oC/Im1vb24i4oC/InN0YXIi4oC/ImFzdGVyb2lkIgpsIOKJjSDijYvijYsgbA==">↗️</a><pre> <span class='Value'>l</span> <span class='Function'>≍</span> <span class='Function'>⍋⍋</span> <span class='Value'>l</span> ┌─ ╵ "planet" "moon" "star" "asteroid" 2 1 3 0 @@ -176,7 +176,7 @@ ┘ </pre> <p>Here we order a table by its second column. Maybe in this case it's not a problem if "dog" and "pig" trade places. But unpredictability is never good—would you get the same results with a different implementation of BQN? And for many other applications of Grade the ordering of equal elements is important. So BQN specifies that matching cells are always ordered by their indices. The same rule applies for Grade Down, so that for example the grade in <em>either</em> direction of an array <code><span class='Value'>𝕩</span></code> where all cells are the same is <code><span class='Function'>↕≠</span><span class='Value'>𝕩</span></code>. One effect is that <code><span class='Function'>⍋</span><span class='Value'>𝕩</span></code> is not always the same as <code><span class='Function'>⌽⍒</span><span class='Value'>𝕩</span></code>, even though <code><span class='Function'>∧</span><span class='Value'>𝕩</span></code> always matches <code><span class='Function'>⌽∨</span><span class='Value'>𝕩</span></code>. And in the table below we can see that the numbers are all reversed but "dog" and "pig" stay in the same order.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KDHiio/LmHQpIOKNkuKKuOKKjyB0">↗️</a><pre> <span class='Paren'>(</span><span class='Number'>1</span><span class='Function'>⊏</span><span class='Modifier'>˘</span><span class='Value'>t</span><span class='Paren'>)</span> <span class='Function'>⍒</span><span class='Modifier2'>⊸</span><span class='Function'>⊏</span> <span class='Value'>t</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHQg4oaQIFsgImRvZyLigL80LCAiYW50IuKAvzYsICJwaWdlb24i4oC/MiwgInBpZyLigL80IF0KKDHiio/LmHQpIOKNkuKKuOKKjyB0">↗️</a><pre> <span class='Paren'>(</span><span class='Number'>1</span><span class='Function'>⊏</span><span class='Modifier'>˘</span><span class='Value'>t</span><span class='Paren'>)</span> <span class='Function'>⍒</span><span class='Modifier2'>⊸</span><span class='Function'>⊏</span> <span class='Value'>t</span> ┌─ ╵ "ant" 6 "dog" 4 diff --git a/docs/doc/pick.html b/docs/doc/pick.html index 060dcdc1..d6e42149 100644 --- a/docs/doc/pick.html +++ b/docs/doc/pick.html @@ -72,7 +72,7 @@ </pre> <h2 id="many-elements"><a class="header" href="#many-elements">Many elements</a></h2> <p>Pick also accepts a list of indices:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSAgIyBEZWZpbmVkIGFib3ZlCgrin6gy4oC/MCwgMeKAv8KvMSwgM+KAvzEsIMKvMeKAv8KvMeKfqSDiipEgYQ==">↗️</a><pre> <span class='Value'>a</span> <span class='Comment'># Defined above +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICdhJyArIOKliuKfnCjihpXDl8K0KSA04oC/NQphICAjIERlZmluZWQgYWJvdmUKCuKfqDLigL8wLCAx4oC/wq8xLCAz4oC/MSwgwq8x4oC/wq8x4p+pIOKKkSBh">↗️</a><pre> <span class='Value'>a</span> <span class='Comment'># Defined above </span>┌─ ╵"abcde fghij @@ -94,7 +94,7 @@ </span>"cbac" </pre> <p>It's much more general than just a list of indices though. As long as your indices are lists, you can arrange them in any array structure with arbitrary nesting.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oMuKAvzAsIOKfqOKfqDHigL/CrzEsIDPigL8x4p+pLCDCrzHigL/CrzHin6nin6kg4oqRIGEKCijin6gy4oC/MCwgMeKAv8KvMeKfqeKJjeKfqDPigL8xLCDCrzHigL/CrzHin6kpIOKKkSBhCgoo4p+oMuKAvzAsIDwx4oC/wq8x4p+p4omN4p+oPDPigL8xLCDCrzHigL/CrzHin6kpIOKKkSBh">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Separator'>,</span> <span class='Bracket'>⟨⟨</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>¯1</span><span class='Separator'>,</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Bracket'>⟩</span><span class='Separator'>,</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'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICdhJyArIOKliuKfnCjihpXDl8K0KSA04oC/NQrin6gy4oC/MCwg4p+o4p+oMeKAv8KvMSwgM+KAvzHin6ksIMKvMeKAv8KvMeKfqeKfqSDiipEgYQoKKOKfqDLigL8wLCAx4oC/wq8x4p+p4omN4p+oM+KAvzEsIMKvMeKAv8KvMeKfqSkg4oqRIGEKCijin6gy4oC/MCwgPDHigL/CrzHin6niiY3in6g8M+KAvzEsIMKvMeKAv8KvMeKfqSkg4oqRIGE=">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Separator'>,</span> <span class='Bracket'>⟨⟨</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>¯1</span><span class='Separator'>,</span> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Bracket'>⟩</span><span class='Separator'>,</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'>a</span> ⟨ 'k' ⟨ "jq" 't' ⟩ ⟩ <span class='Paren'>(</span><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'>¯1</span><span class='Bracket'>⟩</span><span class='Function'>≍</span><span class='Bracket'>⟨</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>¯1</span><span class='Ligature'>‿</span><span class='Number'>¯1</span><span class='Bracket'>⟩</span><span class='Paren'>)</span> <span class='Function'>⊑</span> <span class='Value'>a</span> @@ -114,7 +114,7 @@ ┘ </pre> <p>This option is easily described using the <a href="depth.html#the-depth-modifier">Depth modifier</a>. Pick applies to depth-1 components of the left argument and the entire right argument, which corresponds to a depth operand of <code><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>∞</span></code>. The left argument components have to be lists of numbers, or Pick gives an error.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKfqDLigL8wLCA8MeKAv8KvMeKfqeKJjeKfqDwz4oC/MSwgwq8x4oC/wq8x4p+pKSDiipHimocx4oC/4oieIGEKCuKfqOKfqDIsM+KfqSwx4p+pIOKKkSBhICAjIDEgaXNuJ3QgYSB2YWxpZCBpbmRleA==">↗️</a><pre> <span class='Paren'>(</span><span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Separator'>,</span> <span class='Function'><</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='Bracket'>⟨</span><span class='Function'><</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>¯1</span><span class='Ligature'>‿</span><span class='Number'>¯1</span><span class='Bracket'>⟩</span><span class='Paren'>)</span> <span class='Function'>⊑</span><span class='Modifier2'>⚇</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>∞</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICdhJyArIOKliuKfnCjihpXDl8K0KSA04oC/NQoo4p+oMuKAvzAsIDwx4oC/wq8x4p+p4omN4p+oPDPigL8xLCDCrzHigL/CrzHin6kpIOKKkeKahzHigL/iiJ4gYQoK4p+o4p+oMiwz4p+pLDHin6kg4oqRIGEgICMgMSBpc24ndCBhIHZhbGlkIGluZGV4">↗️</a><pre> <span class='Paren'>(</span><span class='Bracket'>⟨</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Separator'>,</span> <span class='Function'><</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='Bracket'>⟨</span><span class='Function'><</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Number'>¯1</span><span class='Ligature'>‿</span><span class='Number'>¯1</span><span class='Bracket'>⟩</span><span class='Paren'>)</span> <span class='Function'>⊑</span><span class='Modifier2'>⚇</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>∞</span> <span class='Value'>a</span> ┌─ ╵ 'k' ┌· ·'j' diff --git a/docs/doc/quick.html b/docs/doc/quick.html index 3b89af8c..af70a7b7 100644 --- a/docs/doc/quick.html +++ b/docs/doc/quick.html @@ -62,7 +62,7 @@ <p>The function <code><span class='Function'>Lower</span></code> is defined to be <code><span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Value'>diff</span></code>, but it uses a different arrow <code><span class='Gets'>⇐</span></code> to do this. This is an <a href="namespace.html#exports">export</a>, and it declares that <code><span class='Function'>Lower</span></code> is a <em>field</em> of a namespace that can be accessed from the outside. Having a <code><span class='Gets'>⇐</span></code> in it is also what makes the block define a namespace. <code><span class='Function'>Lower</span></code> itself won't be used for a while, but <code><span class='Function'>Upper</span></code> is accessed a few lines down, with <code><span class='Value'>case.</span><span class='Function'>Upper</span></code>.</p> <p><code><span class='Function'>Lower</span></code> is created with a modifier again, this time the 2-modifier <code><span class='Modifier2'>⟜</span></code>. We've now seen one each of the three <a href="primitive.html"><em>primitive</em></a> types: function, 1-modifier, and 2-modifier. There are a lot of primitives, but some simple rules tell you which type they have. Primitives are functions by default, but superscript characters are 1-modifiers (<code><span class='Modifier'>´⁼˘¨˜`</span></code> in our program), and ones with an unbroken circle are 2-modifiers (<code><span class='Modifier2'>⟜∘⌾</span></code>; <code><span class='Function'>⍉</span></code> is a broken circle so it's a function). Variable names follow a <a href="expression.html#role-spellings">similar system</a>, where functions start with an uppercase letter and subjects with a lowercase one.</p> <p><a href="hook.html">After</a> (<code><span class='Modifier2'>⟜</span></code>) takes two operands, <code><span class='Function'>-</span></code> and <code><span class='Value'>diff</span></code>, to produce a function—specifically, it binds <code><span class='Value'>diff</span></code> as the right argument of <code><span class='Function'>-</span></code>, so that calling it on an argument subtracts <code><span class='Value'>diff</span></code> from that argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LeKfnGRpZmYgJ0cnCgonRycgLSBkaWZm">↗️</a><pre> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Value'>diff</span> <span class='String'>'G'</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ZGlmZiDihpAgLcK0ICJBYSIKLeKfnGRpZmYgJ0cnCgonRycgLSBkaWZm">↗️</a><pre> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Value'>diff</span> <span class='String'>'G'</span> 'g' <span class='String'>'G'</span> <span class='Function'>-</span> <span class='Value'>diff</span> @@ -147,7 +147,7 @@ </span>⟨ "Hello" "World" ⟩ </pre> <p>That converts the first character of each string to uppercase! <code><span class='Value'>case.</span><span class='Function'>Upper</span></code> is the case conversion function defined before, so that part makes sense. The rest of the function, <code><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊑</span><span class='Modifier'>¨</span><span class='Paren'>)</span></code>, would be pronounced "<a href="under.html">Under</a> the <a href="pick.html#first">First</a> of <a href="map.html#one-argument-mapping">Each</a>", which… pretty much makes sense too? The First Each function extracts the first element of each list in <code><span class='Value'>hw</span></code>, the part that used to be <code><span class='String'>"hw"</span></code> but is now <code><span class='String'>"HW"</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqRwqggaHcKCmNhc2UuVXBwZXIgImh3Ig==">↗️</a><pre> <span class='Function'>⊑</span><span class='Modifier'>¨</span> <span class='Value'>hw</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcg4oaQIDzLmCAy4oC/4oiYIOKliiAiaGVsbG93b3JsZCIKaHcgY2FzZS5VcHBlcuKMvijiipHCqCnihqkgICMgU2V0cyBuZXcgdmFsdWUgZm9yIGh3CuKKkcKoIGh3CgpjYXNlLlVwcGVyICJodyI=">↗️</a><pre> <span class='Function'>⊑</span><span class='Modifier'>¨</span> <span class='Value'>hw</span> "HW" <span class='Value'>case.</span><span class='Function'>Upper</span> <span class='String'>"hw"</span> @@ -163,14 +163,14 @@ <pre><span class='Function'>•Out</span> <span class='Value'>hw</span> <span class='Gets'>↩</span> <span class='Function'>∾</span> <span class='Function'>⥊⍉</span> <span class='Bracket'>[</span><span class='Value'>hw</span><span class='Separator'>,</span> <span class='String'>", "</span><span class='Ligature'>‿</span><span class='String'>"!"</span><span class='Bracket'>]</span> <span class='Comment'># Hello, World! </span></pre> <p>None of these functions have a subject to the left, so they're all evaluated as prefix functions. But first, we have the <a href="arrayrepr.html#high-rank-arrays">array notation</a> <code><span class='Bracket'>[]</span></code>. This syntax forms an array from its major cells <code><span class='Value'>hw</span></code> and <code><span class='String'>", "</span><span class='Ligature'>‿</span><span class='String'>"!"</span></code> (another strand, a list of two strings). Because the major cells are both lists, we have another rank 2 array.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=W2h3LCAiLCAi4oC/IiEiXQ==">↗️</a><pre> <span class='Bracket'>[</span><span class='Value'>hw</span><span class='Separator'>,</span> <span class='String'>", "</span><span class='Ligature'>‿</span><span class='String'>"!"</span><span class='Bracket'>]</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcg4oaQIDzLmCAy4oC/4oiYIOKliiAiaGVsbG93b3JsZCIKaHcgY2FzZS5VcHBlcuKMvijiipHCqCnihqkgICMgU2V0cyBuZXcgdmFsdWUgZm9yIGh3CltodywgIiwgIuKAvyIhIl0=">↗️</a><pre> <span class='Bracket'>[</span><span class='Value'>hw</span><span class='Separator'>,</span> <span class='String'>", "</span><span class='Ligature'>‿</span><span class='String'>"!"</span><span class='Bracket'>]</span> ┌─ ╵ "Hello" "World" ", " "!" ┘ </pre> <p>The reason for forming this array is to interleave the elements, or we might say to read down the columns rather than across the rows. This ordering is done with a <a href="transpose.html">Transpose</a> to exchange the two axes, then a <a href="reshape.html#deshape">Deshape</a> to flatten it out, giving a list.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4o2JIFtodywgIiwgIuKAvyIhIl0KCuKliiDijYkgW2h3LCAiLCAi4oC/IiEiXQ==">↗️</a><pre> <span class='Function'>⍉</span> <span class='Bracket'>[</span><span class='Value'>hw</span><span class='Separator'>,</span> <span class='String'>", "</span><span class='Ligature'>‿</span><span class='String'>"!"</span><span class='Bracket'>]</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcg4oaQIDzLmCAy4oC/4oiYIOKliiAiaGVsbG93b3JsZCIKaHcgY2FzZS5VcHBlcuKMvijiipHCqCnihqkgICMgU2V0cyBuZXcgdmFsdWUgZm9yIGh3CuKNiSBbaHcsICIsICLigL8iISJdCgripYog4o2JIFtodywgIiwgIuKAvyIhIl0=">↗️</a><pre> <span class='Function'>⍉</span> <span class='Bracket'>[</span><span class='Value'>hw</span><span class='Separator'>,</span> <span class='String'>", "</span><span class='Ligature'>‿</span><span class='String'>"!"</span><span class='Bracket'>]</span> ┌─ ╵ "Hello" ", " "World" "!" @@ -180,7 +180,7 @@ ⟨ "Hello" ", " "World" "!" ⟩ </pre> <p>Finally, <a href="join.html">Join</a> combines this list of strings into a single string.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcg4oapIOKIviDipYrijYkgW2h3LCAiLCAi4oC/IiEiXQpodw==">↗️</a><pre> <span class='Value'>hw</span> <span class='Gets'>↩</span> <span class='Function'>∾</span> <span class='Function'>⥊⍉</span> <span class='Bracket'>[</span><span class='Value'>hw</span><span class='Separator'>,</span> <span class='String'>", "</span><span class='Ligature'>‿</span><span class='String'>"!"</span><span class='Bracket'>]</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcg4oaQIDzLmCAy4oC/4oiYIOKliiAiaGVsbG93b3JsZCIKaHcgY2FzZS5VcHBlcuKMvijiipHCqCnihqkgICMgU2V0cyBuZXcgdmFsdWUgZm9yIGh3Cmh3IOKGqSDiiL4g4qWK4o2JIFtodywgIiwgIuKAvyIhIl0KaHc=">↗️</a><pre> <span class='Value'>hw</span> <span class='Gets'>↩</span> <span class='Function'>∾</span> <span class='Function'>⥊⍉</span> <span class='Bracket'>[</span><span class='Value'>hw</span><span class='Separator'>,</span> <span class='String'>", "</span><span class='Ligature'>‿</span><span class='String'>"!"</span><span class='Bracket'>]</span> <span class='Value'>hw</span> "Hello, World!" </pre> @@ -249,7 +249,7 @@ <span class='Paren'>(</span><span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='Function'>GV</span> <span class='Function'>⊔</span><span class='Modifier'>˜</span> <span class='Nothing'>·</span><span class='Function'>+</span><span class='Modifier'>`</span><span class='Function'>GS</span><span class='Paren'>)</span> <span class='Value'>r</span> </pre> <p>The first line here applies <code><span class='Function'>Proc</span></code> to each character and the one before it, using <code><span class='String'>' '</span></code> as the character "before" the first. <code><span class='Function'>Proc</span><span class='Brace'>{</span><span class='Function'>»𝔽</span><span class='Modifier'>¨</span><span class='Function'>⊢</span><span class='Brace'>}</span> <span class='Value'>𝕩</span></code> is a fancy way to write <code><span class='Paren'>(</span><span class='Function'>»</span><span class='Value'>𝕩</span><span class='Paren'>)</span> <span class='Function'>Proc</span><span class='Modifier'>¨</span> <span class='Value'>𝕩</span></code>, which we'll explain in a moment. First, here's what the <a href="shift.html">Nudge</a> function <code><span class='Function'>»</span></code> does.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcKwrtodw==">↗️</a><pre> <span class='Value'>hw</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcg4oaQIDzLmCAy4oC/4oiYIOKliiAiaGVsbG93b3JsZCIKaHcgY2FzZS5VcHBlcuKMvijiipHCqCnihqkgICMgU2V0cyBuZXcgdmFsdWUgZm9yIGh3Cmh3IOKGqSDiiL4g4qWK4o2JIFtodywgIiwgIuKAvyIhIl0KaHcKwrtodw==">↗️</a><pre> <span class='Value'>hw</span> "Hello, World!" <span class='Function'>»</span><span class='Value'>hw</span> " Hello, World" @@ -257,7 +257,7 @@ <p>It moves its argument forward by one, adding a space character (the array's <a href="fill.html">fill</a>) but keeping the same length. This gives the previous characters that we want to use for <code><span class='Function'>Proc</span></code>'s left argument. Here <a href="map.html#each">Each</a> is used with two arguments, so that it iterates over them simultaneously, like a "zip" in some languages.</p> <p>What about the fancy syntax <code><span class='Function'>Proc</span><span class='Brace'>{</span><span class='Function'>»𝔽</span><span class='Modifier'>¨</span><span class='Function'>⊢</span><span class='Brace'>}</span> <span class='Value'>𝕩</span></code>? The block <code><span class='Brace'>{</span><span class='Function'>»𝔽</span><span class='Modifier'>¨</span><span class='Function'>⊢</span><span class='Brace'>}</span></code> is an immediate 1-modifier because it uses <code><span class='Function'>𝔽</span></code> for an <a href="block.html#operands">operand</a> but not the arguments <code><span class='Value'>𝕨</span></code> or <code><span class='Value'>𝕩</span></code>. This means it acts on <code><span class='Function'>Proc</span></code> only, giving <code><span class='Function'>»Proc</span><span class='Modifier'>¨</span><span class='Function'>⊢</span></code>, which is a <a href="train.html">train</a> because it ends in a function <code><span class='Function'>⊢</span></code>. Following the rules for a 3-train, <code><span class='Paren'>(</span><span class='Function'>»Proc</span><span class='Modifier'>¨</span><span class='Function'>⊢</span><span class='Paren'>)</span><span class='Value'>𝕩</span></code> expands to <code><span class='Paren'>(</span><span class='Function'>»</span><span class='Value'>𝕩</span><span class='Paren'>)</span> <span class='Function'>Proc</span><span class='Modifier'>¨</span> <span class='Paren'>(</span><span class='Function'>⊢</span><span class='Value'>𝕩</span><span class='Paren'>)</span></code>, and since <code><span class='Function'>⊢</span></code> is the <a href="identity.html">identity function</a>, <code><span class='Function'>⊢</span><span class='Value'>𝕩</span></code> is <code><span class='Value'>𝕩</span></code>.</p> <p>Since a display of lots of namespaces isn't too enlightening, we'll skip ahead and show what the results of <code><span class='Function'>GV</span></code> and <code><span class='Function'>GS</span></code> on those lists would be. <code><span class='Function'>GV</span></code> turns each character into a string, except it makes a space into the empty string. <code><span class='Function'>GS</span></code> has a <code><span class='Number'>1</span></code> in the places where we want to split the string.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3Ag4oaQICcgJz1odwpndiDihpAgKDEtc3ApIOKlisKoIGh3CmdzIOKGkCBzcCDiiKggwrviirg9IGh3CgpndgoKZ3M=">↗️</a><pre> <span class='Value'>sp</span> <span class='Gets'>←</span> <span class='String'>' '</span><span class='Function'>=</span><span class='Value'>hw</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcg4oaQIDzLmCAy4oC/4oiYIOKliiAiaGVsbG93b3JsZCIKaHcgY2FzZS5VcHBlcuKMvijiipHCqCnihqkgICMgU2V0cyBuZXcgdmFsdWUgZm9yIGh3Cmh3IOKGqSDiiL4g4qWK4o2JIFtodywgIiwgIuKAvyIhIl0Kc3Ag4oaQICcgJz1odwpndiDihpAgKDEtc3ApIOKlisKoIGh3CmdzIOKGkCBzcCDiiKggwrviirg9IGh3CgpndgoKZ3M=">↗️</a><pre> <span class='Value'>sp</span> <span class='Gets'>←</span> <span class='String'>' '</span><span class='Function'>=</span><span class='Value'>hw</span> <span class='Value'>gv</span> <span class='Gets'>←</span> <span class='Paren'>(</span><span class='Number'>1</span><span class='Function'>-</span><span class='Value'>sp</span><span class='Paren'>)</span> <span class='Function'>⥊</span><span class='Modifier'>¨</span> <span class='Value'>hw</span> <span class='Value'>gs</span> <span class='Gets'>←</span> <span class='Value'>sp</span> <span class='Function'>∨</span> <span class='Function'>»</span><span class='Modifier2'>⊸</span><span class='Function'>=</span> <span class='Value'>hw</span> @@ -312,18 +312,18 @@ </svg> <p>There are actually three train groupings here: from right to left, <code><span class='Nothing'>·</span><span class='Function'>+</span><span class='Modifier'>`</span><span class='Function'>GS</span></code>, <code><span class='Function'>GV</span> <span class='Function'>⊔</span><span class='Modifier'>˜</span> <span class='Value'>…</span></code>, and <code><span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='Value'>…</span></code>. Two of them are 2-trains, which apply one function to the result of another, but the one with <code><span class='Function'>⊔</span></code> is a 3-train, applying a function to two results. In the end, functions <code><span class='Function'>GS</span></code> and <code><span class='Function'>GV</span></code> are applied to <code><span class='Value'>r</span></code>. In fact, to evaluate the entire train we can replace these two functions with their results, giving <code><span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='Paren'>(</span><span class='Function'>GV</span> <span class='Value'>r</span><span class='Paren'>)</span> <span class='Function'>⊔</span><span class='Modifier'>˜</span> <span class='Nothing'>·</span><span class='Function'>+</span><span class='Modifier'>`</span><span class='Paren'>(</span><span class='Function'>GS</span> <span class='Value'>r</span><span class='Paren'>)</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oi+wqggZ3Yg4oqUy5wgwrcrYGdz">↗️</a><pre> <span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='Value'>gv</span> <span class='Function'>⊔</span><span class='Modifier'>˜</span> <span class='Nothing'>·</span><span class='Function'>+</span><span class='Modifier'>`</span><span class='Value'>gs</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Z3Yg4oaQICgxLXNwKSDipYrCqCBodwpncyDihpAgc3Ag4oioIMK74oq4PSBodwriiL7CqCBndiDiipTLnCDCtytgZ3M=">↗️</a><pre> <span class='Function'>∾</span><span class='Modifier'>¨</span> <span class='Value'>gv</span> <span class='Function'>⊔</span><span class='Modifier'>˜</span> <span class='Nothing'>·</span><span class='Function'>+</span><span class='Modifier'>`</span><span class='Value'>gs</span> ⟨ "Hel" "lo," "World!" ⟩ </pre> <p>In this expression, <a href="expression.html#nothing">Nothing</a> can be removed without changing the meaning. It's used in the train to force <code><span class='Function'>+</span><span class='Modifier'>`</span></code> to apply to <code><span class='Function'>GS</span></code> as a 2-train instead of also taking <code><span class='Function'>⊔</span><span class='Modifier'>˜</span></code> as a left argument. The <a href="scan.html">Scan</a> <code><span class='Function'>+</span><span class='Modifier'>`</span></code> is a prefix sum, progressively adding up the numbers in <code><span class='Value'>gs</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Z3MKCitgZ3M=">↗️</a><pre> <span class='Value'>gs</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Z3Mg4oaQIHNwIOKIqCDCu+KKuD0gaHcKZ3MKCitgZ3M=">↗️</a><pre> <span class='Value'>gs</span> ⟨ 0 0 0 1 0 0 1 0 0 0 0 0 0 ⟩ <span class='Function'>+</span><span class='Modifier'>`</span><span class='Value'>gs</span> ⟨ 0 0 0 1 1 1 2 2 2 2 2 2 2 ⟩ </pre> <p>The next bit uses <a href="swap.html">Swap</a> to switch the order: <code><span class='Value'>gv</span> <span class='Function'>⊔</span><span class='Modifier'>˜</span> <span class='Function'>+</span><span class='Modifier'>`</span><span class='Value'>gs</span></code> is <code><span class='Paren'>(</span><span class='Function'>+</span><span class='Modifier'>`</span><span class='Value'>gs</span><span class='Paren'>)</span> <span class='Function'>⊔</span> <span class='Value'>gv</span></code>, but sometimes the different order can read better (here it was mostly to squeeze Nothing into the program, I'll admit). <a href="group.html">Group</a> then splits <code><span class='Value'>gv</span></code> up based on the indices given: the first three elements become element 0 of the result, the next three element 1, and the rest element 2.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KCtgZ3MpIOKKlCBndg==">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>+</span><span class='Modifier'>`</span><span class='Value'>gs</span><span class='Paren'>)</span> <span class='Function'>⊔</span> <span class='Value'>gv</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Z3Yg4oaQICgxLXNwKSDipYrCqCBodwpncyDihpAgc3Ag4oioIMK74oq4PSBodwooK2Bncykg4oqUIGd2">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>+</span><span class='Modifier'>`</span><span class='Value'>gs</span><span class='Paren'>)</span> <span class='Function'>⊔</span> <span class='Value'>gv</span> ┌─ · ⟨ "H" "e" "l" ⟩ ⟨ "l" "o" "," ⟩ ⟨ ⟨⟩ "W" "o" "r" "l" "d" "!" ⟩ ┘ diff --git a/docs/doc/range.html b/docs/doc/range.html index 887f9833..52bbc54d 100644 --- a/docs/doc/range.html +++ b/docs/doc/range.html @@ -86,14 +86,14 @@ ⟨ 0 1 2 0 0 0 6 0 ⟩ </pre> <p>Now at any given position the index of the last 1, if there is any, is the <a href="arithmetic.html#additional-arithmetic">maximum</a> of all the adjusted indices so far. That's a <a href="scan.html">scan</a> <code><span class='Function'>⌈</span><span class='Modifier'>`</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oyIYCBiIMOXIOKGleKJoGIKCijijIhgIOKKoiDDlyDihpXiiJjiiaApIGIgICAjIEFzIGEgdGFjaXQgZnVuY3Rpb24=">↗️</a><pre> <span class='Function'>⌈</span><span class='Modifier'>`</span> <span class='Value'>b</span> <span class='Function'>×</span> <span class='Function'>↕≠</span><span class='Value'>b</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDDigL8x4oC/MeKAvzDigL8w4oC/MOKAvzHigL8wCuKMiGAgYiDDlyDihpXiiaBiCgoo4oyIYCDiiqIgw5cg4oaV4oiY4omgKSBiICAgIyBBcyBhIHRhY2l0IGZ1bmN0aW9u">↗️</a><pre> <span class='Function'>⌈</span><span class='Modifier'>`</span> <span class='Value'>b</span> <span class='Function'>×</span> <span class='Function'>↕≠</span><span class='Value'>b</span> ⟨ 0 1 2 2 2 2 6 6 ⟩ <span class='Paren'>(</span><span class='Function'>⌈</span><span class='Modifier'>`</span> <span class='Function'>⊢</span> <span class='Function'>×</span> <span class='Function'>↕</span><span class='Modifier2'>∘</span><span class='Function'>≠</span><span class='Paren'>)</span> <span class='Value'>b</span> <span class='Comment'># As a tacit function </span>⟨ 0 1 2 2 2 2 6 6 ⟩ </pre> <p>Where there aren't any previous 1s, this returns an index of 0, which is the same as the result where there is a 1 at index 0. If it's important to distinguish these possibilities, the indices can be increased by one, so that the result is 0 if there are no 1s, and 1 for a 1 at the start. To bring it back into alignment with the argument, either this result can be decreased by 1 or an initial element can be added to the argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oyIYCBiIMOXIDEgKyDihpXiiaBi">↗️</a><pre> <span class='Function'>⌈</span><span class='Modifier'>`</span> <span class='Value'>b</span> <span class='Function'>×</span> <span class='Number'>1</span> <span class='Function'>+</span> <span class='Function'>↕≠</span><span class='Value'>b</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDDigL8x4oC/MeKAvzDigL8w4oC/MOKAvzHigL8wCuKMiGAgYiDDlyAxICsg4oaV4omgYg==">↗️</a><pre> <span class='Function'>⌈</span><span class='Modifier'>`</span> <span class='Value'>b</span> <span class='Function'>×</span> <span class='Number'>1</span> <span class='Function'>+</span> <span class='Function'>↕≠</span><span class='Value'>b</span> ⟨ 0 2 3 3 3 3 7 7 ⟩ </pre> <h2 id="list-range"><a class="header" href="#list-range">List range</a></h2> diff --git a/docs/doc/rank.html b/docs/doc/rank.html index 631693cf..409fee4b 100644 --- a/docs/doc/rank.html +++ b/docs/doc/rank.html @@ -104,7 +104,7 @@ </pre> <p>What's it mean for Nudge to shift the "entire table"? The block above shows that it shifts downward, but what's really happening is that Nudge treats <code><span class='Value'>𝕩</span></code> as a collection of major cells—its rows—and shifts these. So it adds an entire row and moves the rest of the rows downwards. Nudge Cells appears similar, but it's acting independently on each row, and the values that it moves around are major cells of the row, that is, rank-0 units.</p> <p>Here's an example showing how Cells can be used to shift each row independently, even though it's not possible to shift columns like this (in fact the best way to do that would be to <a href="transpose.html">transpose</a> in order to work on rows). It uses the not-yet-introduced dyadic form of Cells, so you might want to come back to it after reading the next section.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=KOKGkSLiiJjiiJgiKSDiipHiirjCu8uYIGE=">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>↑</span><span class='String'>"∘∘"</span><span class='Paren'>)</span> <span class='Function'>⊑</span><span class='Modifier2'>⊸</span><span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgJ2EnICsgM+KAv+KImCDipYog4oaVMjQgICMgQSBjaGFyYWN0ZXIgYXJyYXkKKOKGkSLiiJjiiJgiKSDiipHiirjCu8uYIGE=">↗️</a><pre> <span class='Paren'>(</span><span class='Function'>↑</span><span class='String'>"∘∘"</span><span class='Paren'>)</span> <span class='Function'>⊑</span><span class='Modifier2'>⊸</span><span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> ┌─ ╵"abcdefgh ∘ijklmno @@ -112,7 +112,7 @@ ┘ </pre> <p>You can also see how Cells splits its argument into rows using a less array-oriented primitive: <a href="enclose.html">Enclose</a> just wraps each row up so that it appears as a separate element in the final result.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=PMuYIGE=">↗️</a><pre> <span class='Function'><</span><span class='Modifier'>˘</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgJ2EnICsgM+KAv+KImCDipYog4oaVMjQgICMgQSBjaGFyYWN0ZXIgYXJyYXkKPMuYIGE=">↗️</a><pre> <span class='Function'><</span><span class='Modifier'>˘</span> <span class='Value'>a</span> ⟨ "abcdefgh" "ijklmnop" "qrstuvwx" ⟩ </pre> <p>Enclose also comes in handy for the following task: join the rows in an array of lists, resulting in an array where each element is a joined row. The obvious guess would be "join cells", <code><span class='Function'>∾</span><span class='Modifier'>˘</span></code>, but it doesn't work, because each <code><span class='Function'>∾</span></code> can return a result with a different length. Cells tries to make each result of <code><span class='Function'>∾</span></code> into a <em>cell</em>, when the problem was to use it as an <em>element</em>. But a 0-cell is an enclosed element, so we can close the gap by applying <code><span class='Function'><</span></code> to a joined list: <code><span class='Function'><</span><span class='Modifier2'>∘</span><span class='Function'>∾</span></code>.</p> @@ -131,7 +131,7 @@ <p>This approach can apply to more complicated functions as well. And because the result of <code><span class='Function'><</span></code> always has the same shape, <code><span class='Bracket'>⟨⟩</span></code>, the function <code><span class='Function'><</span><span class='Modifier2'>∘</span><span class='Function'>𝔽</span><span class='Modifier'>˘</span></code> can never have a shape agreement error. So if <code><span class='Function'>𝔽</span><span class='Modifier'>˘</span></code> fails, it can't hurt to check <code><span class='Function'><</span><span class='Modifier2'>∘</span><span class='Function'>𝔽</span><span class='Modifier'>˘</span></code> and see what results <code><span class='Function'>𝔽</span></code> is returning.</p> <h3 id="two-arguments"><a class="header" href="#two-arguments">Two arguments</a></h3> <p>When given two arguments, Cells tries to pair their cells together. Starting simple, a unit (whether array or atom) on either side will be paired with every cell of the other argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=J+KImCcgwrvLmCBh">↗️</a><pre> <span class='String'>'∘'</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgJ2EnICsgM+KAv+KImCDipYog4oaVMjQgICMgQSBjaGFyYWN0ZXIgYXJyYXkKJ+KImCcgwrvLmCBh">↗️</a><pre> <span class='String'>'∘'</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> ┌─ ╵"∘abcdefg ∘ijklmno @@ -139,7 +139,7 @@ ┘ </pre> <p>If you <em>want</em> to use this one-to-many behavior with an array, it'll take more work: since you're really only mapping over one argument, <a href="hook.html">bind</a> the other inside Cells.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IuKImOKImCIgwrvLmCBhCgoi4oiY4oiYIuKKuMK7y5ggYQ==">↗️</a><pre> <span class='String'>"∘∘"</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgJ2EnICsgM+KAv+KImCDipYog4oaVMjQgICMgQSBjaGFyYWN0ZXIgYXJyYXkKIuKImOKImCIgwrvLmCBhCgoi4oiY4oiYIuKKuMK7y5ggYQ==">↗️</a><pre> <span class='String'>"∘∘"</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Error'>Error: ˘: Leading axis of arguments not equal (⟨2⟩ ≡ ≢𝕨, 3‿8 ≡ ≢𝕩)</span> <span class='String'>"∘∘"</span><span class='Modifier2'>⊸</span><span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> @@ -150,7 +150,7 @@ ┘ </pre> <p>This is because the general case of Cells does one-to-one matching, pairing the first axis of one argument with the other. For this to work, the two arguments need to have the same length.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4p+oICIwMTIiIMK7y5ggYSwgICgz4oC/4oiY4qWKIlVWV1hZWiIpIMK7y5ggYSDin6k=">↗️</a><pre> <span class='Bracket'>⟨</span> <span class='String'>"012"</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span><span class='Separator'>,</span> <span class='Paren'>(</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Modifier2'>∘</span><span class='Function'>⥊</span><span class='String'>"UVWXYZ"</span><span class='Paren'>)</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Bracket'>⟩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgJ2EnICsgM+KAv+KImCDipYog4oaVMjQgICMgQSBjaGFyYWN0ZXIgYXJyYXkK4p+oICIwMTIiIMK7y5ggYSwgICgz4oC/4oiY4qWKIlVWV1hZWiIpIMK7y5ggYSDin6k=">↗️</a><pre> <span class='Bracket'>⟨</span> <span class='String'>"012"</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span><span class='Separator'>,</span> <span class='Paren'>(</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Modifier2'>∘</span><span class='Function'>⥊</span><span class='String'>"UVWXYZ"</span><span class='Paren'>)</span> <span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> <span class='Bracket'>⟩</span> ┌─ · ┌─ ┌─ ╵"0abcdefg ╵"UVabcdef @@ -160,7 +160,7 @@ ┘ </pre> <p>The arguments might have different ranks: for example, <code><span class='String'>"012"</span></code> has rank 1 and <code><span class='Value'>a</span></code> has rank 2 above. That's fine: it just means Cells will pass arguments of rank 0 and 1 to its operand. You can see these arguments using <a href="pair.html">Pair</a> Cells, <code><span class='Function'>⋈</span><span class='Modifier'>˘</span></code>, so that each cell of the result is just a list of the two arguments used for that call.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IjAxMiIg4ouIy5ggYQ==">↗️</a><pre> <span class='String'>"012"</span> <span class='Function'>⋈</span><span class='Modifier'>˘</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgJ2EnICsgM+KAv+KImCDipYog4oaVMjQgICMgQSBjaGFyYWN0ZXIgYXJyYXkKIjAxMiIg4ouIy5ggYQ==">↗️</a><pre> <span class='String'>"012"</span> <span class='Function'>⋈</span><span class='Modifier'>˘</span> <span class='Value'>a</span> ┌─ ╵ ┌· "abcdefgh" ·'0' @@ -196,7 +196,7 @@ ⟨ "abc" "def" ⟩ </pre> <p>The function <code><span class='Function'>𝔽</span><span class='Modifier2'>⎉</span><span class='Value'>k</span></code>, for <code><span class='Value'>k</span><span class='Function'>≥</span><span class='Number'>0</span></code>, operates on the <code><span class='Value'>k</span></code>-cells of its arguments—that is, it maps over all <em>but</em> the last <code><span class='Value'>k</span></code> axes. For any given argument <code><span class='Value'>a</span></code>, ranks <code><span class='Value'>k</span></code> and <code><span class='Value'>k</span><span class='Function'>-=</span><span class='Value'>a</span></code> are the same, as long as <code><span class='Value'>k</span><span class='Function'>≥</span><span class='Number'>0</span></code> and <code><span class='Paren'>(</span><span class='Value'>k</span><span class='Function'>-=</span><span class='Value'>a</span><span class='Paren'>)</span><span class='Function'>≤</span><span class='Number'>¯1</span></code>. So rank 2 is rank ¯3 for a rank-5 array. The reason this option is useful is that the same rank might be applied to multiple arguments, either with multiple function calls or one call on two arguments. Let's revisit an example with Cells from before, shifting the same string into each row of a table. The function <code><span class='Function'>»</span></code> should be called on rank-1 strings, but because the argument ranks are different, a negative rank can't get down to rank 1 on both sides. Positive rank 1 does the job, allowing us to unbundle the string <code><span class='String'>"∘∘"</span></code> so that <code><span class='Function'>»</span><span class='Modifier2'>⎉</span><span class='Number'>1</span></code> is a standalone function.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IuKImOKImCLiirjCu8uYIGEKCiLiiJjiiJgiIMK74o6JMSBh">↗️</a><pre> <span class='String'>"∘∘"</span><span class='Modifier2'>⊸</span><span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgJ2EnICsgM+KAv+KImCDipYog4oaVMjQgICMgQSBjaGFyYWN0ZXIgYXJyYXkKIuKImOKImCLiirjCu8uYIGEKCiLiiJjiiJgiIMK74o6JMSBh">↗️</a><pre> <span class='String'>"∘∘"</span><span class='Modifier2'>⊸</span><span class='Function'>»</span><span class='Modifier'>˘</span> <span class='Value'>a</span> ┌─ ╵"∘∘abcdef ∘∘ijklmn @@ -260,7 +260,7 @@ ⟨ 2 ¯1 3 ⟩ </pre> <p>But a rank of <code><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>∞</span></code> works the best because it also defines a matrix-<em>matrix</em> product. Which as shown below does the same transformation to the <em>cells</em> of the right-hand-side matrix, instead of the elements of a vector. This works because <code><span class='Function'>×</span></code> and <code><span class='Function'>+</span><span class='Modifier'>˝</span></code> work on the leading axes of their arguments. When <code><span class='Modifier2'>⎉</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>∞</span></code> is applied, these axes are the last axis of <code><span class='Value'>𝕨</span></code> and the first axis of <code><span class='Value'>𝕩</span></code>. Which… is kind of weird, but it's what a matrix product is.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=K8udIDDigL8x4oC/MCDDlyAx4oC/MuKAvzPDl+KMnDHigL8xMAoKbSAry53iiJjDl+KOiTHigL/iiJ4gMeKAvzLigL8zw5fijJwx4oC/MTA=">↗️</a><pre> <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'>0</span> <span class='Function'>×</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Function'>×</span><span class='Modifier'>⌜</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>10</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIG0g4oaQIFsw4oC/MeKAvzAsIMKvMeKAvzDigL8wLCAw4oC/MOKAvzFdCivLnSAw4oC/MeKAvzAgw5cgMeKAvzLigL8zw5fijJwx4oC/MTAKCm0gK8ud4oiYw5fijokx4oC/4oieIDHigL8y4oC/M8OX4oycMeKAvzEw">↗️</a><pre> <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'>0</span> <span class='Function'>×</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Function'>×</span><span class='Modifier'>⌜</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>10</span> ⟨ 2 20 ⟩ <span class='Value'>m</span> <span class='Function'>+</span><span class='Modifier'>˝</span><span class='Modifier2'>∘</span><span class='Function'>×</span><span class='Modifier2'>⎉</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>∞</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Function'>×</span><span class='Modifier'>⌜</span><span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>10</span> diff --git a/docs/doc/rebqn.html b/docs/doc/rebqn.html index a3d0c97e..36c0439c 100644 --- a/docs/doc/rebqn.html +++ b/docs/doc/rebqn.html @@ -27,7 +27,7 @@ ⟨ 0 1 2 3 4 5 6 7 8 ⟩ </pre> <p>Options can be used in any combination. Here's a calculator REPL:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Y2FsY1JlcGwg4oaQIOKAolJlQlFOIHtyZXBs4oeQInN0cmljdCIsIHByaW1pdGl2ZXPih5BjYWxjRm5zfQoKQ2FsY1JlcGwgImIg4oaQIDEgLSBh4oaQNiIKCkNhbGNSZXBsICJhICogYiI=">↗️</a><pre> <span class='Value'>calcRepl</span> <span class='Gets'>←</span> <span class='Function'>•ReBQN</span> <span class='Brace'>{</span><span class='Value'>repl</span><span class='Gets'>⇐</span><span class='String'>"strict"</span><span class='Separator'>,</span> <span class='Value'>primitives</span><span class='Gets'>⇐</span><span class='Value'>calcFns</span><span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cmVwbCDihpAg4oCiUmVCUU4ge3JlcGzih5Aic3RyaWN0In0gICMgTG93ZXJjYXNlIGZvciBmdW5jdGlvbiByZXN1bHQKY2FsY0ZucyDihpAg4p+oJysn4oC/KywgJy0n4oC/LSwgJyon4oC/w5csICcvJ+KAvyjihpXiipjDtynin6kKY2FsY1JlcGwg4oaQIOKAolJlQlFOIHtyZXBs4oeQInN0cmljdCIsIHByaW1pdGl2ZXPih5BjYWxjRm5zfQoKQ2FsY1JlcGwgImIg4oaQIDEgLSBh4oaQNiIKCkNhbGNSZXBsICJhICogYiI=">↗️</a><pre> <span class='Value'>calcRepl</span> <span class='Gets'>←</span> <span class='Function'>•ReBQN</span> <span class='Brace'>{</span><span class='Value'>repl</span><span class='Gets'>⇐</span><span class='String'>"strict"</span><span class='Separator'>,</span> <span class='Value'>primitives</span><span class='Gets'>⇐</span><span class='Value'>calcFns</span><span class='Brace'>}</span> <span class='Function'>CalcRepl</span> <span class='String'>"b ← 1 - a←6"</span> ¯5 @@ -37,19 +37,19 @@ </pre> <h2 id="repl-mode"><a class="header" href="#repl-mode">REPL mode</a></h2> <p>The <code><span class='Value'>repl</span></code> property can have the values <code><span class='String'>"none"</span></code>, <code><span class='String'>"strict"</span></code>, and <code><span class='String'>"loose"</span></code>. If no value is given it's equivalent to <code><span class='String'>"none"</span></code>, which means that the resulting function has no memory and each evaluation is independent from the others. But the values <code><span class='String'>"strict"</span></code> and <code><span class='String'>"loose"</span></code> make evaluations take place in a shared <a href="lexical.html">scope</a>. Now a variable defined at the top level of one source string is visible when later ones are evaluated, and can be viewed and modified.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ZG8g4oaQIOKAolJlQlFOIHtyZXBs4oeQImxvb3NlIn0KCkRvwqggImHihpA0IuKAvyLin6hhLGLihpA14p+pIuKAvyJ74p+oYeKGqfCdlaksYuKfqX04Ig==">↗️</a><pre> <span class='Value'>do</span> <span class='Gets'>←</span> <span class='Function'>•ReBQN</span> <span class='Brace'>{</span><span class='Value'>repl</span><span class='Gets'>⇐</span><span class='String'>"loose"</span><span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cmVwbCDihpAg4oCiUmVCUU4ge3JlcGzih5Aic3RyaWN0In0gICMgTG93ZXJjYXNlIGZvciBmdW5jdGlvbiByZXN1bHQKZG8g4oaQIOKAolJlQlFOIHtyZXBs4oeQImxvb3NlIn0KCkRvwqggImHihpA0IuKAvyLin6hhLGLihpA14p+pIuKAvyJ74p+oYeKGqfCdlaksYuKfqX04Ig==">↗️</a><pre> <span class='Value'>do</span> <span class='Gets'>←</span> <span class='Function'>•ReBQN</span> <span class='Brace'>{</span><span class='Value'>repl</span><span class='Gets'>⇐</span><span class='String'>"loose"</span><span class='Brace'>}</span> <span class='Function'>Do</span><span class='Modifier'>¨</span> <span class='String'>"a←4"</span><span class='Ligature'>‿</span><span class='String'>"⟨a,b←5⟩"</span><span class='Ligature'>‿</span><span class='String'>"{⟨a↩𝕩,b⟩}8"</span> ⟨ 4 ⟨ 4 5 ⟩ ⟨ 8 5 ⟩ ⟩ </pre> <p>A different <code><span class='Function'>•ReBQN</span></code> result has its own scope and can't access these variables.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=ZG9Ob3Qg4oaQIOKAolJlQlFOIHtyZXBs4oeQImxvb3NlIn0KCkRvTm90ICJiIiAjIHN1cnByaXNlZCB3aGVuIHRoaXMgZmFpbHM=">↗️</a><pre> <span class='Value'>doNot</span> <span class='Gets'>←</span> <span class='Function'>•ReBQN</span> <span class='Brace'>{</span><span class='Value'>repl</span><span class='Gets'>⇐</span><span class='String'>"loose"</span><span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cmVwbCDihpAg4oCiUmVCUU4ge3JlcGzih5Aic3RyaWN0In0gICMgTG93ZXJjYXNlIGZvciBmdW5jdGlvbiByZXN1bHQKZG9Ob3Qg4oaQIOKAolJlQlFOIHtyZXBs4oeQImxvb3NlIn0KCkRvTm90ICJiIiAjIHN1cnByaXNlZCB3aGVuIHRoaXMgZmFpbHM=">↗️</a><pre> <span class='Value'>doNot</span> <span class='Gets'>←</span> <span class='Function'>•ReBQN</span> <span class='Brace'>{</span><span class='Value'>repl</span><span class='Gets'>⇐</span><span class='String'>"loose"</span><span class='Brace'>}</span> <span class='Function'>DoNot</span> <span class='String'>"b"</span> <span class='Comment'># surprised when this fails </span><span class='Error'>Error: Undefined identifier</span> </pre> <p>The difference in <code><span class='String'>"strict"</span></code> and <code><span class='String'>"loose"</span></code> is that a loose REPL can define a variable again, which just changes its value (under the covers, the <code><span class='Gets'>←</span></code> is treated as a <code><span class='Gets'>↩</span></code>).</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=RG8gImEg4oaQIMKvMSIKRG8gImEg4oaQIGLigL9hIgoKKOKAolJlQlFOIHtyZXBs4oeQInN0cmljdCJ9KeKOikDCqCAiYeKGkDEi4oC/ImHihpAyIiAgIyBTZWNvbmQgb25lIGVycm9ycw==">↗️</a><pre> <span class='Function'>Do</span> <span class='String'>"a ← ¯1"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cmVwbCDihpAg4oCiUmVCUU4ge3JlcGzih5Aic3RyaWN0In0gICMgTG93ZXJjYXNlIGZvciBmdW5jdGlvbiByZXN1bHQKZG8g4oaQIOKAolJlQlFOIHtyZXBs4oeQImxvb3NlIn0KRG8gImEg4oaQIMKvMSIKRG8gImEg4oaQIGLigL9hIgoKKOKAolJlQlFOIHtyZXBs4oeQInN0cmljdCJ9KeKOikDCqCAiYeKGkDEi4oC/ImHihpAyIiAgIyBTZWNvbmQgb25lIGVycm9ycw==">↗️</a><pre> <span class='Function'>Do</span> <span class='String'>"a ← ¯1"</span> ¯1 <span class='Function'>Do</span> <span class='String'>"a ← b‿a"</span> ⟨ 5 ¯1 ⟩ diff --git a/docs/doc/replicate.html b/docs/doc/replicate.html index 1e1fabc5..6ffec63c 100644 --- a/docs/doc/replicate.html +++ b/docs/doc/replicate.html @@ -149,7 +149,7 @@ ┘ </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'><</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Function'><</span><span class='Number'>3</span><span class='Bracket'>⟩</span> <span class='Function'>/</span> <span class='Value'>b</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDLigL81IOKliiDihpUxMArin6g8Miw8M+KfqSAvIGI=">↗️</a><pre> <span class='Bracket'>⟨</span><span class='Function'><</span><span class='Number'>2</span><span class='Separator'>,</span><span class='Function'><</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 @@ -158,7 +158,7 @@ ┘ </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> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDLigL81IOKliiDihpUxMArin6gyLDPin6kgLyBi">↗️</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 @@ -169,7 +169,7 @@ </pre> <p>The example above has a different result from the previous one! The function <code><span class='Function'><</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> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDLigL81IOKliiDihpUxMApiIOKJoSDin6jin6kgLyBi">↗️</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> @@ -243,7 +243,7 @@ <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> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHIg4oaQIDHCqOKMvigz4oC/OOKKj+KliikgM+KAvzbipYowCi/il4vipYrin5wo4oaV4omiKSBy">↗️</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> diff --git a/docs/doc/reshape.html b/docs/doc/reshape.html index 68537bf0..e6039f3b 100644 --- a/docs/doc/reshape.html +++ b/docs/doc/reshape.html @@ -75,7 +75,7 @@ ⟨ 135 136 137 145 146 147 235 236 237 245 246 247 ⟩ </pre> <p>The elements are ordered in reading order—left to right, then top to bottom. This means that leading axes "matter more" for ordering: if one element comes earlier in the first axis but later in the second than some other element, it will come first in the result. In another view, elements are ordered according to their <a href="indices.html">indices</a>, leading to the name <em>index order</em> for this ordering. To be precise, deshaping the array of indices for an array always gives a <a href="order.html">sorted</a> array.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oaV4omiYQoK4o2LIOKliiDihpXiiaJh">↗️</a><pre> <span class='Function'>↕≢</span><span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICvijJzCtCDin6gxMDDigL8yMDAsIDMw4oC/NDAsIDXigL824oC/N+KfqQrihpXiiaJhCgrijYsg4qWKIOKGleKJomE=">↗️</a><pre> <span class='Function'>↕≢</span><span class='Value'>a</span> ┌─ ╎ ⟨ 0 0 0 ⟩ ⟨ 0 0 1 ⟩ ⟨ 0 0 2 ⟩ ⟨ 0 1 0 ⟩ ⟨ 0 1 1 ⟩ ⟨ 0 1 2 ⟩ @@ -99,7 +99,7 @@ <p>The left argument of Reshape gives the shape of the result, except that one entry can be left unspecified for BQN to fill in. We'll look at the cases where a full shape is given first.</p> <h3 id="matching-lengths"><a class="header" href="#matching-lengths">Matching lengths</a></h3> <p>If the number of elements implied by the given shape—that is, <code><span class='Function'>×</span><span class='Modifier'>´</span><span class='Value'>𝕨</span></code>—is equal to the number of elements in <code><span class='Value'>𝕩</span></code>, then <code><span class='Value'>𝕩</span></code> is simply rearranged to match that shape. The element list is kept the same, so that the deshaped result matches the deshaped right argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YQoKNuKAvzIg4qWKIGEKCijipYphKSDiiaEg4qWKIDbigL8y4qWKYQ==">↗️</a><pre> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICvijJzCtCDin6gxMDDigL8yMDAsIDMw4oC/NDAsIDXigL824oC/N+KfqQphCgo24oC/MiDipYogYQoKKOKlimEpIOKJoSDipYogNuKAvzLipYph">↗️</a><pre> <span class='Value'>a</span> ┌─ ╎ 135 136 137 145 146 147 @@ -135,7 +135,7 @@ </pre> <h3 id="non-matching-lengths"><a class="header" href="#non-matching-lengths">Non-matching lengths</a></h3> <p>If <code><span class='Value'>𝕨</span></code> implies a smaller number of elements than are present initially, then only the initial elements of <code><span class='Value'>𝕩</span></code> are used. Here the result stops at <code><span class='Number'>237</span></code>, three-quarters of the way through <code><span class='Value'>a</span></code>, because at that point the result is filled up.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=M+KAvzMg4qWKIGE=">↗️</a><pre> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>3</span> <span class='Function'>⥊</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICvijJzCtCDin6gxMDDigL8yMDAsIDMw4oC/NDAsIDXigL824oC/N+KfqQoz4oC/MyDipYogYQ==">↗️</a><pre> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>3</span> <span class='Function'>⥊</span> <span class='Value'>a</span> ┌─ ╵ 135 136 137 145 146 147 @@ -143,7 +143,7 @@ ┘ </pre> <p>If <code><span class='Value'>𝕨</span></code> implies a larger number of elements, then elements of <code><span class='Value'>𝕩</span></code> are reused cyclically. Below, we reach the last element <code><span class='Number'>247</span></code> and start over at <code><span class='Number'>135</span></code>. If <code><span class='Value'>𝕩</span></code> doesn't have any elements to start with, you'll get an error as there aren't any elements available.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MTUg4qWKIGEKCjQg4qWKIOKGlTA=">↗️</a><pre> <span class='Number'>15</span> <span class='Function'>⥊</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQICvijJzCtCDin6gxMDDigL8yMDAsIDMw4oC/NDAsIDXigL824oC/N+KfqQoxNSDipYogYQoKNCDipYog4oaVMA==">↗️</a><pre> <span class='Number'>15</span> <span class='Function'>⥊</span> <span class='Value'>a</span> ⟨ 135 136 137 145 146 147 235 236 237 245 246 247 135 136 137 ⟩ <span class='Number'>4</span> <span class='Function'>⥊</span> <span class='Function'>↕</span><span class='Number'>0</span> diff --git a/docs/doc/reverse.html b/docs/doc/reverse.html index cd984689..4d594095 100644 --- a/docs/doc/reverse.html +++ b/docs/doc/reverse.html @@ -94,7 +94,7 @@ <span class='Error'>Error: 𝕨⌽𝕩: Length of compound 𝕨 must be at most rank of 𝕩</span> </pre> <p>The expression below rotates the first (vertical) axis of <code><span class='Value'>tab</span></code> by one element, and second by two. So the line of capital letters goes from being one away from the top, up to the top, and the column with <code><span class='String'>'2'</span></code> goes from horizontal index 2 to index 0.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MeKAvzIg4oy9IHRhYg==">↗️</a><pre> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Function'>⌽</span> <span class='Value'>tab</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHRhYiDihpAgM+KAvzTipYoiYWJjZEFCQ0QwMTIzIgox4oC/MiDijL0gdGFi">↗️</a><pre> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span> <span class='Function'>⌽</span> <span class='Value'>tab</span> ┌─ ╵"CDAB 2301 @@ -102,7 +102,7 @@ ┘ </pre> <p>The vertical and horizontal rotations are independent, and could also be done with two <code><span class='Function'>⌽</span></code>s and a <code><span class='Modifier'>˘</span></code>. The multi-axis form is more convenient, and can potentially be evaluated faster than multiple separate rotations in the cases where it shows up.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MSDijL0gMiDijL3LmCB0YWI=">↗️</a><pre> <span class='Number'>1</span> <span class='Function'>⌽</span> <span class='Number'>2</span> <span class='Function'>⌽</span><span class='Modifier'>˘</span> <span class='Value'>tab</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHRhYiDihpAgM+KAvzTipYoiYWJjZEFCQ0QwMTIzIgoxIOKMvSAyIOKMvcuYIHRhYg==">↗️</a><pre> <span class='Number'>1</span> <span class='Function'>⌽</span> <span class='Number'>2</span> <span class='Function'>⌽</span><span class='Modifier'>˘</span> <span class='Value'>tab</span> ┌─ ╵"CDAB 2301 diff --git a/docs/doc/scan.html b/docs/doc/scan.html index bac2f4c5..9856380b 100644 --- a/docs/doc/scan.html +++ b/docs/doc/scan.html @@ -143,7 +143,7 @@ ┘ </pre> <p>If <code><span class='Value'>𝕨</span></code> is given, it must have the same shape as a <a href="array.html#cells">major cell</a> of <code><span class='Value'>𝕩</span></code> (this is why <code><span class='Value'>𝕨</span></code> needs to be enclosed when <code><span class='Value'>𝕩</span></code> is a list: in general it's an array). Then the first result cell is found by applying <code><span class='Function'>𝔽</span></code> to elements of <code><span class='Value'>𝕨</span></code> and <code><span class='Function'>⊏</span><span class='Value'>𝕩</span></code>, and the computation continues as in the one-argument case for remaining cells.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=M+KAvzLigL8x4oC/MCArYCBh">↗️</a><pre> <span class='Number'>3</span><span class='Ligature'>‿</span><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='Function'>+</span><span class='Modifier'>`</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIMKvMuKAvzAuMjXigL8nYSfigL/iiJ4g4oi+IDPigL804qWKwq8x4oC/MOKAvzEKM+KAvzLigL8x4oC/MCArYCBh">↗️</a><pre> <span class='Number'>3</span><span class='Ligature'>‿</span><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='Function'>+</span><span class='Modifier'>`</span> <span class='Value'>a</span> ┌─ ╵ 1 2.25 'b' ∞ 0 2.25 'c' ∞ diff --git a/docs/doc/search.html b/docs/doc/search.html index a92b25c1..948891e1 100644 --- a/docs/doc/search.html +++ b/docs/doc/search.html @@ -200,21 +200,21 @@ ⟨ 0 2 ⟩ </pre> <p>The first thing you might try to search for just one element does not go so well (and yes, this <a href="../commentary/problems.html#search-function-depth">is a bad thing</a>).</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3R1ZmYg4oqQICJzdHJpbmci">↗️</a><pre> <span class='Value'>stuff</span> <span class='Function'>⊐</span> <span class='String'>"string"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3R1ZmYg4oaQICJ0YWNrcyLigL8icGFwZXIi4oC/InN0cmluZyLigL8idGFwZSIKc3R1ZmYg4oqQICJzdHJpbmci">↗️</a><pre> <span class='Value'>stuff</span> <span class='Function'>⊐</span> <span class='String'>"string"</span> ⟨ 4 4 4 4 4 4 ⟩ </pre> <p>Instead of interpreting <code><span class='Value'>𝕩</span></code> as a single element, Index of treats it as a list, and <code><span class='Value'>𝕨</span></code> doesn't even contain characters! Well, <a href="enclose.html">Enclose</a> (<code><span class='Function'><</span></code>) makes an array from a single element…</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3R1ZmYg4oqQPCAic3RyaW5nIg==">↗️</a><pre> <span class='Value'>stuff</span> <span class='Function'>⊐<</span> <span class='String'>"string"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3R1ZmYg4oaQICJ0YWNrcyLigL8icGFwZXIi4oC/InN0cmluZyLigL8idGFwZSIKc3R1ZmYg4oqQPCAic3RyaW5nIg==">↗️</a><pre> <span class='Value'>stuff</span> <span class='Function'>⊐<</span> <span class='String'>"string"</span> ┌· · 2 ┘ </pre> <p>This result has the right information, but is enclosed and could break the program later on. Remember that the result of a search function is <em>always</em> an array. We really want the <a href="pick.html#first">first</a> element.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3R1ZmYg4oqR4oiY4oqQ4p+cPCAic3RyaW5nIg==">↗️</a><pre> <span class='Value'>stuff</span> <span class='Function'>⊑</span><span class='Modifier2'>∘</span><span class='Function'>⊐</span><span class='Modifier2'>⟜</span><span class='Function'><</span> <span class='String'>"string"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3R1ZmYg4oaQICJ0YWNrcyLigL8icGFwZXIi4oC/InN0cmluZyLigL8idGFwZSIKc3R1ZmYg4oqR4oiY4oqQ4p+cPCAic3RyaW5nIg==">↗️</a><pre> <span class='Value'>stuff</span> <span class='Function'>⊑</span><span class='Modifier2'>∘</span><span class='Function'>⊐</span><span class='Modifier2'>⟜</span><span class='Function'><</span> <span class='String'>"string"</span> 2 </pre> <p>If <code><span class='Value'>𝕨</span></code> is fixed, then the version I prefer is to use <a href="under.html">Under</a> to enclose the argument and then un-enclose the result. It requires <code><span class='Value'>𝕨</span></code> to be bound to <code><span class='Function'>⊐</span></code> because otherwise Under would enclose <code><span class='Value'>𝕨</span></code> as well, since it applies <code><span class='Function'>𝔾</span></code> to both arguments.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3R1ZmbiirjiipDijL48ICJzdHJpbmci">↗️</a><pre> <span class='Value'>stuff</span><span class='Modifier2'>⊸</span><span class='Function'>⊐</span><span class='Modifier2'>⌾</span><span class='Function'><</span> <span class='String'>"string"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c3R1ZmYg4oaQICJ0YWNrcyLigL8icGFwZXIi4oC/InN0cmluZyLigL8idGFwZSIKc3R1ZmbiirjiipDijL48ICJzdHJpbmci">↗️</a><pre> <span class='Value'>stuff</span><span class='Modifier2'>⊸</span><span class='Function'>⊐</span><span class='Modifier2'>⌾</span><span class='Function'><</span> <span class='String'>"string"</span> 2 </pre> <p>For Member of, the equivalent is <code><span class='Function'>∊</span><span class='Modifier2'>⟜</span><span class='Value'>stuff</span><span class='Modifier2'>⌾</span><span class='Function'><</span></code>.</p> diff --git a/docs/doc/select.html b/docs/doc/select.html index 566f6484..978da307 100644 --- a/docs/doc/select.html +++ b/docs/doc/select.html @@ -117,7 +117,7 @@ </pre> <p>More generally, <code><span class='Value'>𝕨</span></code> can be an array of any rank. Each of its 0-cells—containing a single number—is replaced with a cell of <code><span class='Value'>𝕩</span></code> in the result. The result's shape is then made up of the shape of <code><span class='Value'>𝕨</span></code> and the major cell shape of <code><span class='Value'>𝕩</span></code>: it's <code><span class='Paren'>(</span><span class='Function'>≢</span><span class='Value'>𝕨</span><span class='Paren'>)</span><span class='Function'>∾</span><span class='Number'>1</span><span class='Function'>↓≢</span><span class='Value'>𝕩</span></code>.</p> <p>When <code><span class='Value'>𝕩</span></code> is a list, the result has the same shape as <code><span class='Value'>𝕨</span></code>. Elements of <code><span class='Value'>𝕨</span></code> are replaced one-for-one with elements 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=MnxtCgooMnxtKSDiio8gIiAqIg==">↗️</a><pre> <span class='Number'>2</span><span class='Function'>|</span><span class='Value'>m</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIG0g4oaQIDPigL814oC/N+KAvzExIHzijJwgw5fLnOKGlTcKMnxtCgooMnxtKSDiio8gIiAqIg==">↗️</a><pre> <span class='Number'>2</span><span class='Function'>|</span><span class='Value'>m</span> ┌─ ╵ 0 1 1 0 1 1 0 0 1 0 0 1 0 1 diff --git a/docs/doc/selfcmp.html b/docs/doc/selfcmp.html index 05490758..d4cb1cbc 100644 --- a/docs/doc/selfcmp.html +++ b/docs/doc/selfcmp.html @@ -202,7 +202,7 @@ ⟨ 0 1 2 ⟩ </pre> <p>Applying both separately is a different story, and gives completely interesting results. These results contain all information from the original argument, as <code><span class='Function'>⍷</span></code> indicates which cells it contained and <code><span class='Function'>⊐</span></code> indicates where they were located. The function <a href="select.html">Select</a> (<code><span class='Function'>⊏</span></code>) reconstructs the argument from the two values.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4o23IGMK4oqQIGMKKOKKkGMpIOKKjyAo4o23Yyk=">↗️</a><pre> <span class='Function'>⍷</span> <span class='Value'>c</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGMg4oaQID4ieWVsbG93IuKAvyJvcmFuZ2Ui4oC/InllbGxvdyLigL8icHVycGxlIuKAvyJvcmFuZ2Ui4oC/InllbGxvdyIK4o23IGMK4oqQIGMKKOKKkGMpIOKKjyAo4o23Yyk=">↗️</a><pre> <span class='Function'>⍷</span> <span class='Value'>c</span> ┌─ ╵"yellow orange diff --git a/docs/doc/shape.html b/docs/doc/shape.html index 50140270..eef019ab 100644 --- a/docs/doc/shape.html +++ b/docs/doc/shape.html @@ -31,7 +31,7 @@ </span>4 </pre> <p>The length is the first element of the shape, and the rank is the length of the shape—the number of axes. For another example, taking the first (and only) cell of <code><span class='Value'>arr</span></code> gives an array with shape <code><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>6</span></code>, length <code><span class='Number'>3</span></code>, and rank <code><span class='Number'>3</span></code>, as we can see by applying <a href="map.html#each">each</a> function to <code><span class='Function'>⊏</span><span class='Value'>arr</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omi4oC/PeKAv+KJoCB78J2VjvCdlal9wqg8IOKKj2Fycg==">↗️</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='Function'>⊏</span><span class='Value'>arr</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGFyciDihpAgMeKAvzPigL8y4oC/NiDipYogJzAnK+KGlTEwCuKJouKAvz3igL/iiaAge/CdlY7wnZWpfcKoPCDiio9hcnI=">↗️</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='Function'>⊏</span><span class='Value'>arr</span> ⟨ ⟨ 3 2 6 ⟩ 3 3 ⟩ </pre> <p>Applying Shape and the other two functions to an atom shows a shape of <code><span class='Bracket'>⟨⟩</span></code> (the empty list), and a rank of zero and length of 1. The same is true of an enclosed array, which like an atom is a kind of unit.</p> diff --git a/docs/doc/shift.html b/docs/doc/shift.html index 84684f8a..a1e8aa25 100644 --- a/docs/doc/shift.html +++ b/docs/doc/shift.html @@ -36,7 +36,7 @@ </span>⟨ 1 2 2 4 3 5 6 ⟩ </pre> <p>In this way <code><span class='Function'>»</span></code> refers to a sequence containing the previous element at each position. By default the array's fill is used for the element before the first, and a right argument can be given to provide a different one.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oieIMK7IHMKCuKKj+KKuMK7IHM=">↗️</a><pre> <span class='Number'>∞</span> <span class='Function'>»</span> <span class='Value'>s</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cyDihpAgMeKAvzLigL8y4oC/NOKAvzPigL814oC/NgriiJ4gwrsgcwoK4oqP4oq4wrsgcw==">↗️</a><pre> <span class='Number'>∞</span> <span class='Function'>»</span> <span class='Value'>s</span> ⟨ ∞ 1 2 2 4 3 5 ⟩ <span class='Function'>⊏</span><span class='Modifier2'>⊸</span><span class='Function'>»</span> <span class='Value'>s</span> @@ -44,7 +44,7 @@ </pre> <p>It may appear backwards that <code><span class='Function'>»</span></code>, which typically means "go to the next item", is used to represent the previous item. In fact there is no conflict: the symbol <code><span class='Function'>»</span></code> describes what position each cell of <code><span class='Value'>𝕩</span></code> will have in the result, but in this context we are interested in knowing what argument value occurs in a particular result position. By moving all numbers into the future we ensure that a number in the present comes from the past. To keep your intuition functioning in these situations, it may help to think of the arrow point as fixed at some position in the result while the tail stretches back to land on the argument position where it comes from.</p> <p>Switching the direction of the arrow, we get an operation that pulls the next value into each position:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cyDiiY0gwqtzCsKr4oq4LSBz">↗️</a><pre> <span class='Value'>s</span> <span class='Function'>≍</span> <span class='Function'>«</span><span class='Value'>s</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cyDihpAgMeKAvzLigL8y4oC/NOKAvzPigL814oC/NgpzIOKJjSDCq3MKwqviirgtIHM=">↗️</a><pre> <span class='Value'>s</span> <span class='Function'>≍</span> <span class='Function'>«</span><span class='Value'>s</span> ┌─ ╵ 1 2 2 4 3 5 6 2 2 4 3 5 6 0 @@ -53,7 +53,7 @@ ⟨ 1 0 2 ¯1 2 1 ¯6 ⟩ </pre> <p>The differences here are the same as <code><span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Function'>»</span> <span class='Value'>s</span></code>, except that they are shifted over by one, and it is the <em>last</em> value in the sequence that is compared with a fill value, not the first. These techniques adapt easily to more complicated operations. A symmetric difference is found by subtracting the previous element from the next, and dividing by two:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MsO3y5wgKMK7LcKrKSBzCgoyw7fLnCAo4oqjy53iirjCuyAtIOKKosud4oq4wqspIHMgICMgUmVwZWF0IGF0IHRoZSBlbmRzIGluc3RlYWQgb2YgdXNpbmcgZmlsbHM=">↗️</a><pre> <span class='Number'>2</span><span class='Function'>÷</span><span class='Modifier'>˜</span> <span class='Paren'>(</span><span class='Function'>»-«</span><span class='Paren'>)</span> <span class='Value'>s</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=cyDihpAgMeKAvzLigL8y4oC/NOKAvzPigL814oC/Ngoyw7fLnCAowrstwqspIHMKCjLDt8ucICjiiqPLneKKuMK7IC0g4oqiy53iirjCqykgcyAgIyBSZXBlYXQgYXQgdGhlIGVuZHMgaW5zdGVhZCBvZiB1c2luZyBmaWxscw==">↗️</a><pre> <span class='Number'>2</span><span class='Function'>÷</span><span class='Modifier'>˜</span> <span class='Paren'>(</span><span class='Function'>»-«</span><span class='Paren'>)</span> <span class='Value'>s</span> ⟨ ¯1 ¯0.5 ¯1 ¯0.5 ¯0.5 ¯1.5 2.5 ⟩ <span class='Number'>2</span><span class='Function'>÷</span><span class='Modifier'>˜</span> <span class='Paren'>(</span><span class='Function'>⊣</span><span class='Modifier'>˝</span><span class='Modifier2'>⊸</span><span class='Function'>»</span> <span class='Function'>-</span> <span class='Function'>⊢</span><span class='Modifier'>˝</span><span class='Modifier2'>⊸</span><span class='Function'>«</span><span class='Paren'>)</span> <span class='Value'>s</span> <span class='Comment'># Repeat at the ends instead of using fills @@ -72,7 +72,7 @@ </span>⟨ 1 1 0 1 1 0 0 0 ⟩ </pre> <p>With a number in big-endian format, a right shift might be logical, shifting in zeros, or arithmetic, shifting in copies of the highest-order bit (for little-endian numbers, this applies to left shifts rather than right ones). The two kinds of shift can be performed with similar code, using <code><span class='Number'>0</span></code> or <code><span class='Function'>⊏</span><span class='Value'>𝕩</span></code> for the inserted cell.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MyDipYrin5ww4oq4wrsgaSAgICAjIExvZ2ljYWwgcmlnaHQgc2hpZnQKCjMgKOKliuKfnOKKj8K74oqiKSBpICAjIEFyaXRobWV0aWMgcmlnaHQgc2hpZnQ=">↗️</a><pre> <span class='Number'>3</span> <span class='Function'>⥊</span><span class='Modifier2'>⟜</span><span class='Number'>0</span><span class='Modifier2'>⊸</span><span class='Function'>»</span> <span class='Value'>i</span> <span class='Comment'># Logical right shift +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGkg4oaQICIxMDAxMTAxMSItJzAnCjMg4qWK4p+cMOKKuMK7IGkgICAgIyBMb2dpY2FsIHJpZ2h0IHNoaWZ0CgozICjipYrin5ziio/Cu+KKoikgaSAgIyBBcml0aG1ldGljIHJpZ2h0IHNoaWZ0">↗️</a><pre> <span class='Number'>3</span> <span class='Function'>⥊</span><span class='Modifier2'>⟜</span><span class='Number'>0</span><span class='Modifier2'>⊸</span><span class='Function'>»</span> <span class='Value'>i</span> <span class='Comment'># Logical right shift </span>⟨ 0 0 0 1 0 0 1 1 ⟩ <span class='Number'>3</span> <span class='Paren'>(</span><span class='Function'>⥊</span><span class='Modifier2'>⟜</span><span class='Function'>⊏»⊢</span><span class='Paren'>)</span> <span class='Value'>i</span> <span class='Comment'># Arithmetic right shift diff --git a/docs/doc/take.html b/docs/doc/take.html index 1c976976..e141a489 100644 --- a/docs/doc/take.html +++ b/docs/doc/take.html @@ -125,7 +125,7 @@ </pre> <p>Now Take and Drop taken together don't include the whole array. Take includes the elements that are selected on <em>every</em> axis, while Drop excludes the ones selected on <em>any</em> axis. They are opposite corners that meet at some point in the middle of the array (here, at the spot between <code><span class='Number'>2</span></code> and <code><span class='Number'>11</span></code>).</p> <p>Any integer values at all can be used, in any combination. Here one axis is shortened and the other's padded with fills. The result of Take has shape <code><span class='Function'>|</span><span class='Value'>𝕨</span></code>, maybe plus some trailing axes from <code><span class='Value'>𝕩</span></code>. Of course, if that's too big for your available memory, your BQN implementation probably can't compute it for you!</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=M+KAv8KvMTIg4oaRIG0KCuKJoiA54oC/wq80IOKGkSDihpU34oC/NuKAvzUgICMgVHJhaWxpbmcgc2hhcGUgZXhhbXBsZQ==">↗️</a><pre> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>¯12</span> <span class='Function'>↑</span> <span class='Value'>m</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIG0g4oaQICgxMMOX4oaVNSkgK+KMnCDihpU3CjPigL/CrzEyIOKGkSBtCgriiaIgOeKAv8KvNCDihpEg4oaVN+KAvzbigL81ICAjIFRyYWlsaW5nIHNoYXBlIGV4YW1wbGU=">↗️</a><pre> <span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>¯12</span> <span class='Function'>↑</span> <span class='Value'>m</span> ┌─ ╵ 0 0 0 0 0 0 1 2 3 4 5 6 0 0 0 0 0 10 11 12 13 14 15 16 diff --git a/docs/doc/train.html b/docs/doc/train.html index 61e253ea..1f7c1f5b 100644 --- a/docs/doc/train.html +++ b/docs/doc/train.html @@ -52,20 +52,20 @@ ⟨ 0 1 2 3 0 0 4 1 3 5 6 ⟩ </pre> <p>Each <code><span class='String'>'t'</span></code> is <code><span class='Number'>0</span></code>, each <code><span class='String'>'a'</span></code> is <code><span class='Number'>1</span></code>, and so on. We'd like to discard some of the information from Classify, to just find whether each major cell had a new value. Here are the input and desired result:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c2Mg4omNIOKIiiAidGFjaXR0cmFpbnMi">↗️</a><pre> <span class='Value'>sc</span> <span class='Function'>≍</span> <span class='Function'>∊</span> <span class='String'>"tacittrains"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHNjIOKGkCDiipAgInRhY2l0dHJhaW5zIgpzYyDiiY0g4oiKICJ0YWNpdHRyYWlucyI=">↗️</a><pre> <span class='Value'>sc</span> <span class='Function'>≍</span> <span class='Function'>∊</span> <span class='String'>"tacittrains"</span> ┌─ ╵ 0 1 2 3 0 0 4 1 3 5 6 1 1 1 1 0 0 1 0 0 1 1 ┘ </pre> <p>The result should be <code><span class='Number'>1</span></code> when a new number appears, higher than all the previous numbers. To do this, we first find the highest previous number by taking the <a href="arithmetic.html#additional-arithmetic">maximum</a>-<a href="scan.html">scan</a> <code><span class='Function'>⌈</span><span class='Modifier'>`</span></code> of the argument, then <a href="shift.html">shifting</a> to move the previous maximum to the current position. The first cell is always new, so we shift in a <code><span class='Number'>¯1</span></code>, so it will be less than any element of the argument.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=wq8xIMK7IOKMiGBzYwoowq8xwrvijIhgKSBzYw==">↗️</a><pre> <span class='Number'>¯1</span> <span class='Function'>»</span> <span class='Function'>⌈</span><span class='Modifier'>`</span><span class='Value'>sc</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHNjIOKGkCDiipAgInRhY2l0dHJhaW5zIgrCrzEgwrsg4oyIYHNjCijCrzHCu+KMiGApIHNj">↗️</a><pre> <span class='Number'>¯1</span> <span class='Function'>»</span> <span class='Function'>⌈</span><span class='Modifier'>`</span><span class='Value'>sc</span> ⟨ ¯1 0 1 2 3 3 3 4 4 4 5 ⟩ <span class='Paren'>(</span><span class='Number'>¯1</span><span class='Function'>»⌈</span><span class='Modifier'>`</span><span class='Paren'>)</span> <span class='Value'>sc</span> ⟨ ¯1 0 1 2 3 3 3 4 4 4 5 ⟩ </pre> <p>Now we compare the original list with the list of previous-maximums.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=c2MgPiDCrzHCu+KMiGBzYwoo4oqiPsKvMcK74oyIYCkgc2M=">↗️</a><pre> <span class='Value'>sc</span> <span class='Function'>></span> <span class='Number'>¯1</span><span class='Function'>»⌈</span><span class='Modifier'>`</span><span class='Value'>sc</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIHNjIOKGkCDiipAgInRhY2l0dHJhaW5zIgpzYyA+IMKvMcK74oyIYHNjCijiiqI+wq8xwrvijIhgKSBzYw==">↗️</a><pre> <span class='Value'>sc</span> <span class='Function'>></span> <span class='Number'>¯1</span><span class='Function'>»⌈</span><span class='Modifier'>`</span><span class='Value'>sc</span> ⟨ 1 1 1 1 0 0 1 0 0 1 1 ⟩ <span class='Paren'>(</span><span class='Function'>⊢></span><span class='Number'>¯1</span><span class='Function'>»⌈</span><span class='Modifier'>`</span><span class='Paren'>)</span> <span class='Value'>sc</span> ⟨ 1 1 1 1 0 0 1 0 0 1 1 ⟩ diff --git a/docs/doc/transpose.html b/docs/doc/transpose.html index 8c9841b3..998840b8 100644 --- a/docs/doc/transpose.html +++ b/docs/doc/transpose.html @@ -21,7 +21,7 @@ ┘ </pre> <p>Transpose is named this way because it exchanges the two axes of the matrix. Above you can see that while <code><span class='Value'>mat</span></code> has shape <code><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>3</span></code>, <code><span class='Function'>⍉</span><span class='Value'>mat</span></code> has shape <code><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span></code>, and we can also check that the element at <a href="indices.html">index</a> <code><span class='Value'>i</span><span class='Ligature'>‿</span><span class='Value'>j</span></code> in <code><span class='Value'>mat</span></code> is the same as the one at <code><span class='Value'>j</span><span class='Ligature'>‿</span><span class='Value'>i</span></code> in <code><span class='Function'>⍉</span><span class='Value'>mat</span></code>:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MeKAvzAg4oqRIG1hdAow4oC/MSDiipEg4o2JIG1hdA==">↗️</a><pre> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>⊑</span> <span class='Value'>mat</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIG1hdCDihpAgMuKAvzMg4qWKIOKGlTYKMeKAvzAg4oqRIG1hdAow4oC/MSDiipEg4o2JIG1hdA==">↗️</a><pre> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>0</span> <span class='Function'>⊑</span> <span class='Value'>mat</span> 3 <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> <span class='Function'>⊑</span> <span class='Function'>⍉</span> <span class='Value'>mat</span> 3 @@ -53,7 +53,7 @@ ┘ </pre> <p>But, ignoring the whitespace and going in reading order, the argument and result have exactly the same element ordering as for the rank 2 matrix <code><span class='Function'>⥊</span><span class='Modifier'>˘</span> <span class='Value'>a322</span></code>:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4ouI4p+c4o2JIOKlisuYIGEzMjI=">↗️</a><pre> <span class='Function'>⋈</span><span class='Modifier2'>⟜</span><span class='Function'>⍉</span> <span class='Function'>⥊</span><span class='Modifier'>˘</span> <span class='Value'>a322</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YTMyMiDihpAgM+KAvzLigL8y4qWK4oaVMTIK4ouI4p+c4o2JIOKlisuYIGEzMjI=">↗️</a><pre> <span class='Function'>⋈</span><span class='Modifier2'>⟜</span><span class='Function'>⍉</span> <span class='Function'>⥊</span><span class='Modifier'>˘</span> <span class='Value'>a322</span> ┌─ · ┌─ ┌─ ╵ 0 1 2 3 ╵ 0 4 8 @@ -64,7 +64,7 @@ ┘ </pre> <p>To exchange multiple axes, use the <a href="repeat.html">Repeat</a> modifier. A negative power moves axes in the other direction, just like how <a href="reverse.html#rotate">Rotate</a> handles negative left arguments. In particular, to move the last axis to the front, use <a href="undo.html">Undo</a> (as you might expect, this exactly inverts <code><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=4omiIOKNieKNnzMgYTIzNDU2CgriiaIg4o2J4oG8IGEyMzQ1Ng==">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier2'>⍟</span><span class='Number'>3</span> <span class='Value'>a23456</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIOKNieKNnzMgYTIzNDU2CgriiaIg4o2J4oG8IGEyMzQ1Ng==">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier2'>⍟</span><span class='Number'>3</span> <span class='Value'>a23456</span> ⟨ 5 6 2 3 4 ⟩ <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier'>⁼</span> <span class='Value'>a23456</span> @@ -72,11 +72,11 @@ </pre> <p>In fact, we have <code><span class='Function'>≢⍉</span><span class='Modifier2'>⍟</span><span class='Value'>k</span> <span class='Value'>a</span> <span class='Gets'>←→</span> <span class='Value'>k</span><span class='Function'>⌽≢</span><span class='Value'>a</span></code> for any whole number <code><span class='Value'>k</span></code> and array <code><span class='Value'>a</span></code>.</p> <p>To move axes other than the first, use the <a href="rank.html">Rank modifier</a> in order to leave initial axes untouched. A rank of <code><span class='Value'>k</span><span class='Function'>></span><span class='Number'>0</span></code> transposes only the last <code><span class='Value'>k</span></code> axes while a rank of <code><span class='Value'>k</span><span class='Function'><</span><span class='Number'>0</span></code> ignores the first <code><span class='Function'>|</span><span class='Value'>k</span></code> axes.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKNieKOiTMgYTIzNDU2">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier2'>⎉</span><span class='Number'>3</span> <span class='Value'>a23456</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIOKNieKOiTMgYTIzNDU2">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier2'>⎉</span><span class='Number'>3</span> <span class='Value'>a23456</span> ⟨ 2 3 5 6 4 ⟩ </pre> <p>And of course, Rank and Repeat can be combined to do more complicated transpositions: move a set of contiguous axes with any starting point and length to the end.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKNieKBvOKOicKvMSBhMjM0NTY=">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier'>⁼</span><span class='Modifier2'>⎉</span><span class='Number'>¯1</span> <span class='Value'>a23456</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIOKNieKBvOKOicKvMSBhMjM0NTY=">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier'>⁼</span><span class='Modifier2'>⎉</span><span class='Number'>¯1</span> <span class='Value'>a23456</span> ⟨ 2 6 3 4 5 ⟩ </pre> <p>Using these forms (and the <a href="shape.html">Rank</a> function), we can state BQN's generalized matrix product swapping rule:</p> @@ -84,28 +84,28 @@ </pre> <p>Certainly not as concise as APL's version, but not a horror either. BQN's rule is actually more parsimonious in that it only performs the axis exchanges necessary for the computation: it moves the two axes that will be paired with the matrix product into place before the product, and directly exchanges all axes afterwards. Each of these steps is equivalent in terms of data movement to a matrix transpose, the simplest nontrivial transpose to perform. Also remember that for two-dimensional matrices both kinds of transposition are the same, so that APL's simpler rule <code><span class='Function'>MP</span> <span class='Function'>≡</span> <span class='Function'>MP</span><span class='Modifier2'>⌾</span><span class='Function'>⍉</span><span class='Modifier'>˜</span></code> holds in BQN on rank 2.</p> <p>Axis permutations of the types we've shown generate the complete permutation group on any number of axes, so you could produce any transposition you want with the right sequence of monadic transpositions with Rank. However, this can be unintuitive and tedious. What if you want to transpose the first three axes, leaving the rest alone? With monadic Transpose you have to send some axes to the end, then bring them back to the beginning. For example [following four or five failed tries]:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIOKNieKBvOKOicKvMiDijYkgYTIzNDU2ICAjIFJlc3RyaWN0IFRyYW5zcG9zZSB0byB0aGUgZmlyc3QgdGhyZWUgYXhlcw==">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier'>⁼</span><span class='Modifier2'>⎉</span><span class='Number'>¯2</span> <span class='Function'>⍉</span> <span class='Value'>a23456</span> <span class='Comment'># Restrict Transpose to the first three axes +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIOKNieKBvOKOicKvMiDijYkgYTIzNDU2ICAjIFJlc3RyaWN0IFRyYW5zcG9zZSB0byB0aGUgZmlyc3QgdGhyZWUgYXhlcw==">↗️</a><pre> <span class='Function'>≢</span> <span class='Function'>⍉</span><span class='Modifier'>⁼</span><span class='Modifier2'>⎉</span><span class='Number'>¯2</span> <span class='Function'>⍉</span> <span class='Value'>a23456</span> <span class='Comment'># Restrict Transpose to the first three axes </span>⟨ 3 4 2 5 6 ⟩ </pre> <p>In a case like this the dyadic version of <code><span class='Function'>⍉</span></code>, called Reorder Axes, is much easier.</p> <h2 id="reorder-axes"><a class="header" href="#reorder-axes">Reorder Axes</a></h2> <p>Transpose also allows a left argument that specifies a permutation of <code><span class='Value'>𝕩</span></code>'s axes. For each index <code><span class='Value'>p</span><span class='Gets'>←</span><span class='Value'>i</span><span class='Function'>⊑</span><span class='Value'>𝕨</span></code> in the left argument, axis <code><span class='Value'>i</span></code> of <code><span class='Value'>𝕩</span></code> is used for axis <code><span class='Value'>p</span></code> of the result. Multiple argument axes can be sent to the same result axis, in which case that axis goes along a diagonal of <code><span class='Value'>𝕩</span></code>, and the result will have a lower rank than <code><span class='Value'>𝕩</span></code> (see the next section).</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDHigL8z4oC/MuKAvzDigL80IOKNiSBhMjM0NTYKCuKJoiAx4oC/MuKAvzLigL8w4oC/MCDijYkgYTIzNDU2ICAjIERvbid0IHdvcnJ5IHRvbyBtdWNoIGFib3V0IHRoaXMgY2FzZSB0aG91Z2g=">↗️</a><pre> <span class='Function'>≢</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>4</span> <span class='Function'>⍉</span> <span class='Value'>a23456</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIDHigL8z4oC/MuKAvzDigL80IOKNiSBhMjM0NTYKCuKJoiAx4oC/MuKAvzLigL8w4oC/MCDijYkgYTIzNDU2ICAjIERvbid0IHdvcnJ5IHRvbyBtdWNoIGFib3V0IHRoaXMgY2FzZSB0aG91Z2g=">↗️</a><pre> <span class='Function'>≢</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>4</span> <span class='Function'>⍉</span> <span class='Value'>a23456</span> ⟨ 5 2 4 3 6 ⟩ <span class='Function'>≢</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>2</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='Value'>a23456</span> <span class='Comment'># Don't worry too much about this case though </span>⟨ 5 2 3 ⟩ </pre> <p>Since this kind of rearrangement can be counterintuitive, it's often easier to use <code><span class='Function'>⍉</span><span class='Modifier'>⁼</span></code> when specifying all axes. If <code><span class='Value'>p</span><span class='Function'>≡</span><span class='Modifier2'>○</span><span class='Function'>≠≢</span><span class='Value'>a</span></code>, then we have <code><span class='Function'>≢</span><span class='Value'>p</span><span class='Function'>⍉</span><span class='Modifier'>⁼</span><span class='Value'>a</span> <span class='Gets'>←→</span> <span class='Value'>p</span><span class='Function'>⊏≢</span><span class='Value'>a</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDHigL8z4oC/MuKAvzDigL80IOKNieKBvCBhMjM0NTY=">↗️</a><pre> <span class='Function'>≢</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>4</span> <span class='Function'>⍉</span><span class='Modifier'>⁼</span> <span class='Value'>a23456</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIDHigL8z4oC/MuKAvzDigL80IOKNieKBvCBhMjM0NTY=">↗️</a><pre> <span class='Function'>≢</span> <span class='Number'>1</span><span class='Ligature'>‿</span><span class='Number'>3</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>4</span> <span class='Function'>⍉</span><span class='Modifier'>⁼</span> <span class='Value'>a23456</span> ⟨ 3 5 4 2 6 ⟩ </pre> <p>BQN makes one further extension, which is to allow only some axes to be specified (this is the only difference in dyadic <code><span class='Function'>⍉</span></code> relative to APL). Then <code><span class='Value'>𝕨</span></code> will be matched up with <a href="leading.html">leading axes</a> of <code><span class='Value'>𝕩</span></code>. Those axes are moved according to <code><span class='Value'>𝕨</span></code>, and remaining axes are placed in order into the gaps between them.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDDigL8y4oC/NCDijYkgYTIzNDU2">↗️</a><pre> <span class='Function'>≢</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span> <span class='Function'>⍉</span> <span class='Value'>a23456</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIDDigL8y4oC/NCDijYkgYTIzNDU2">↗️</a><pre> <span class='Function'>≢</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>2</span><span class='Ligature'>‿</span><span class='Number'>4</span> <span class='Function'>⍉</span> <span class='Value'>a23456</span> ⟨ 2 5 3 6 4 ⟩ </pre> <p>In particular, the case with only one axis specified is interesting. Here, the first axis ends up at the given location. This gives us a much better solution to the problem at the end of the last section.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIDIg4o2JIGEyMzQ1NiAgIyBSZXN0cmljdCBUcmFuc3Bvc2UgdG8gdGhlIGZpcnN0IHRocmVlIGF4ZXM=">↗️</a><pre> <span class='Function'>≢</span> <span class='Number'>2</span> <span class='Function'>⍉</span> <span class='Value'>a23456</span> <span class='Comment'># Restrict Transpose to the first three axes +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4omiIGEyMzQ1NiDihpAg4oaVMuKAvzPigL804oC/NeKAvzYK4omiIDIg4o2JIGEyMzQ1NiAgIyBSZXN0cmljdCBUcmFuc3Bvc2UgdG8gdGhlIGZpcnN0IHRocmVlIGF4ZXM=">↗️</a><pre> <span class='Function'>≢</span> <span class='Number'>2</span> <span class='Function'>⍉</span> <span class='Value'>a23456</span> <span class='Comment'># Restrict Transpose to the first three axes </span>⟨ 3 4 2 5 6 ⟩ </pre> <p>Finally, it's worth noting that, as monadic Transpose moves the first axis to the end, it's equivalent to Reorder Axes with a "default" left argument: <code><span class='Paren'>(</span><span class='Function'>=-</span><span class='Number'>1</span><span class='Modifier'>˙</span><span class='Paren'>)</span><span class='Modifier2'>⊸</span><span class='Function'>⍉</span></code>.</p> diff --git a/docs/doc/under.html b/docs/doc/under.html index 2a06e458..4ec761a8 100644 --- a/docs/doc/under.html +++ b/docs/doc/under.html @@ -58,7 +58,7 @@ ┘ </pre> <p>When used with Under, the function <code><span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>⌽</span></code> applies to the first column, <a href="reverse.html#rotate">rotating</a> it. The result of <code><span class='Function'>𝔽</span></code> needs to be compatible with the selection function, so Rotate works but trying to drop an element is no good:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MeKKuOKGk+KMvijiio/LmCkgYQ==">↗️</a><pre> <span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>↓</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊏</span><span class='Modifier'>˘</span><span class='Paren'>)</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGEg4oaQIDTigL8z4qWK4oaVMTIKMeKKuOKGk+KMvijiio/LmCkgYQ==">↗️</a><pre> <span class='Number'>1</span><span class='Modifier2'>⊸</span><span class='Function'>↓</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊏</span><span class='Modifier'>˘</span><span class='Paren'>)</span> <span class='Value'>a</span> <span class='Error'>Error: ⁼: Inverse not found</span> </pre> <p>BQN can detect lots of structural functions when written <a href="tacit.html">tacitly</a>; see the list of recognized forms <a href="../spec/inferred.html#required-structural-inverses">in the spec</a>. You can also include computations on the shape. For example, here's a function to reverse the first half of a list.</p> diff --git a/docs/help/change.html b/docs/help/change.html index 172b4c58..f8b6c103 100644 --- a/docs/help/change.html +++ b/docs/help/change.html @@ -21,12 +21,12 @@ <h2 id="n-f-modify"><a class="header" href="#n-f-modify"><code><span class='Value'>n</span> <span class='Function'>F</span><span class='Gets'>↩</span></code>: Modify</a></h2> <p><a class="fulldoc" href="../doc/expression.html#assignment">→full documentation</a></p> <p>Apply function <code><span class='Function'>F</span></code> to existing variable <code><span class='Value'>n</span></code>, and assign the result back to <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=4oqiIGIg4oy94oap">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>b</span> <span class='Function'>⌽</span><span class='Gets'>↩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDMK4oqiIGIg4oapICJCZSB0aGUgY2hhbmdlIHlvdSB3aXNoIHRvIHNlZSBpbiB0aGUgd29ybGQuIgriiqIgYiDijL3ihqk=">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>b</span> <span class='Function'>⌽</span><span class='Gets'>↩</span> ".dlrow eht ni ees ot hsiw uoy egnahc eht eB" </pre> <h2 id="n-f-v-modify"><a class="header" href="#n-f-v-modify"><code><span class='Value'>n</span> <span class='Function'>F</span><span class='Gets'>↩</span> <span class='Value'>v</span></code>: Modify</a></h2> <p><a class="fulldoc" href="../doc/expression.html#assignment">→full documentation</a></p> <p>Assign <code><span class='Value'>n</span> <span class='Function'>F</span> <span class='Value'>v</span></code> to <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=4oqiIGIg4oaTy5zihqkgNg==">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>b</span> <span class='Function'>↓</span><span class='Modifier'>˜</span><span class='Gets'>↩</span> <span class='Number'>6</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=4oqiIGIg4oaQIDMK4oqiIGIg4oapICJCZSB0aGUgY2hhbmdlIHlvdSB3aXNoIHRvIHNlZSBpbiB0aGUgd29ybGQuIgriiqIgYiDijL3ihqkK4oqiIGIg4oaTy5zihqkgNg==">↗️</a><pre> <span class='Function'>⊢</span> <span class='Value'>b</span> <span class='Function'>↓</span><span class='Modifier'>˜</span><span class='Gets'>↩</span> <span class='Number'>6</span> " eht ni ees ot hsiw uoy egnahc eht eB" </pre> diff --git a/docs/tutorial/variable.html b/docs/tutorial/variable.html index 1bb916ac..5e05d336 100644 --- a/docs/tutorial/variable.html +++ b/docs/tutorial/variable.html @@ -24,11 +24,11 @@ ⟨ 3 7 ⟩ </pre> <p>A variable can't be defined twice in the same <em>scope</em>. Later we'll work with functions and other pieces of code that create their own scopes, but for now all you need to know is that all the code in a tutorial runs in the same scope. So <code><span class='Value'>three</span></code> is already defined, and can't be defined again.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dGhyZWUg4oaQIDQ=">↗️</a><pre> <span class='Value'>three</span> <span class='Gets'>←</span> <span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dGhyZWUg4ouIIHRlbiAtIHRocmVlIOKGkCAzCnRocmVlIOKGkCA0">↗️</a><pre> <span class='Value'>three</span> <span class='Gets'>←</span> <span class='Number'>4</span> <span class='Error'>Error: Redefinition</span> </pre> <p>It's a little crazy to call them variables if the definition can never change, right? Doesn't "variable" <em>mean</em> "able to change"? Fortunately, this is one way in which BQN isn't crazy. You can <em>modify</em> a variable's value with the arrow <code><span class='Gets'>↩</span></code> provided it's already been defined. This never does anything to the original value: that value stays the same; it's just (probably) not the value of the modified variable any more.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dGhyZWUg4oapIDQKCnRocmVlID0gMyAgICMgV2FpdCB3aHkgZGlkIEkgZG8gdGhhdAoKMyA9IHRocmVlIOKGqSAzCgpmb3VyIOKGqSAzICAgICMgZm91ciBpc24ndCBkZWZpbmVkIHlldA==">↗️</a><pre> <span class='Value'>three</span> <span class='Gets'>↩</span> <span class='Number'>4</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dGhyZWUg4ouIIHRlbiAtIHRocmVlIOKGkCAzCnRocmVlIOKGkCA0CnRocmVlIOKGqSA0Cgp0aHJlZSA9IDMgICAjIFdhaXQgd2h5IGRpZCBJIGRvIHRoYXQKCjMgPSB0aHJlZSDihqkgMwoKZm91ciDihqkgMyAgICAjIGZvdXIgaXNuJ3QgZGVmaW5lZCB5ZXQ=">↗️</a><pre> <span class='Value'>three</span> <span class='Gets'>↩</span> <span class='Number'>4</span> <span class='Value'>three</span> <span class='Function'>=</span> <span class='Number'>3</span> <span class='Comment'># Wait why did I do that </span>0 @@ -46,7 +46,7 @@ </pre> <p>Does BQN not know about capital letters? Does it object to self-reference? Why is "<code><span class='Function'>BQN</span></code>" green? At least there's an error message, and a "role" is something we've heard about before. <em>Assignment</em> means anything written with a leftward arrow—either definition or modification.</p> <p>I'll first confuse you a little more by pointing out that BQN's variables are case-insensitive, and even underscore-insensitive!</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dGhyZWUKdGhyRWUKVGhSZUUKdGhyX0VFCl9fdGhyZWUKX1RfSF9SX0VfRV8=">↗️</a><pre> <span class='Value'>three</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dGhyZWUg4ouIIHRlbiAtIHRocmVlIOKGkCAzCnRocmVlIOKGkCA0CnRocmVlIOKGqSA0CjMgPSB0aHJlZSDihqkgMwp0aHJlZQp0aHJFZQpUaFJlRQp0aHJfRUUKX190aHJlZQpfVF9IX1JfRV9FXw==">↗️</a><pre> <span class='Value'>three</span> 3 <span class='Value'>thrEe</span> 3 @@ -60,7 +60,7 @@ 3 </pre> <p>But the syntax highlighter still seems to care, and you'll get a strange result if you try to apply a function to one of the uppercase spellings:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LSBUaHJlZQoKLSBfdGhyZWU=">↗️</a><pre> <span class='Function'>-</span> <span class='Function'>Three</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=dGhyZWUg4ouIIHRlbiAtIHRocmVlIOKGkCAzCnRocmVlIOKGkCA0CnRocmVlIOKGqSA0CjMgPSB0aHJlZSDihqkgMwotIFRocmVlCgotIF90aHJlZQ==">↗️</a><pre> <span class='Function'>-</span> <span class='Function'>Three</span> -3{𝔽} <span class='Function'>-</span> <span class='Modifier'>_three</span> @@ -118,7 +118,7 @@ </pre> <p>This strategy allows us to break down a program into smaller parts. However, you can only name a function in this way, not an expression. We'll explain later how to turn an expression into an explicit function. But one thing remains true regardless of how a function is created: functions are just another kind of BQN value, and giving a function a name uses the ordinary definition arrow <code><span class='Gets'>←</span></code>, not any special syntax.</p> <p>Even if you define a variable to be a function at first, you're not locked in to that choice. You can modify the variable to have a different value (but remember to change the casing to match the new value's role!). If it's a data value, you'll still be able to call it as a function: it will return itself.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QmFzZTIKCmJhc2UyIOKGqSAxNiAgICMgQ2hhbmdlIGl0IHRvIGEgbnVtYmVyCgpCYXNlMgoKQmFzZTIgNg==">↗️</a><pre> <span class='Function'>Base2</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=QmFzZTIg4oaQICvin5woMuKKuMOXKcK04oiY4oy9CkJhc2UyCgpiYXNlMiDihqkgMTYgICAjIENoYW5nZSBpdCB0byBhIG51bWJlcgoKQmFzZTIKCkJhc2UyIDY=">↗️</a><pre> <span class='Function'>Base2</span> +⟜(2⊸×)´∘⌽ <span class='Value'>base2</span> <span class='Gets'>↩</span> <span class='Number'>16</span> <span class='Comment'># Change it to a number @@ -312,7 +312,7 @@ ⟨ 4 5 6 ⟩ </pre> <p>But this changes the value of <code><span class='Value'>a</span></code> to a completely unrelated value. What if I want to apply a transformation to <code><span class='Value'>a</span></code>, for example to subtract one? Of course I can write the value <code><span class='Value'>a</span></code> on the right hand side of the assignment, but that's extra work and doesn't really seem to represent what I'm doing, which is conceptually just to apply a function to <code><span class='Value'>a</span></code>. So BQN also has a shorthand, called <em>modified assignment</em>. Here, the modified assignment <code><span class='Function'>-</span><span class='Gets'>↩</span></code> subtracts a value from <code><span class='Value'>a</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihqkgYSAtIDEKYQoKYSAt4oapIDE=">↗️</a><pre> <span class='Value'>a</span> <span class='Gets'>↩</span> <span class='Value'>a</span> <span class='Function'>-</span> <span class='Number'>1</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphCgphIC3ihqkgMQ==">↗️</a><pre> <span class='Value'>a</span> <span class='Gets'>↩</span> <span class='Value'>a</span> <span class='Function'>-</span> <span class='Number'>1</span> <span class='Value'>a</span> ⟨ 3 4 5 ⟩ @@ -320,31 +320,31 @@ ⟨ 2 3 4 ⟩ </pre> <p>(In case you're wondering why I didn't have to write <code><span class='Value'>a</span></code> again that last time, the evaluator suppresses the printed result for ordinary assignments but not modified ones. This is a feature of my website software and not the BQN language). It looks a lot like the special assignment operators <code><span class='Function'>+=</span></code>, <code><span class='Function'>/=</span></code>, and so on that you'll see in C or Javascript. What BQN brings to the table is that you can use any two-argument function at all here, because two-argument function are always written as operators. For example, we can prepend some elements to <code><span class='Value'>a</span></code>:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDiiL7LnOKGqSAw4oC/MQ==">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>∾</span><span class='Modifier'>˜</span><span class='Gets'>↩</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8x">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>∾</span><span class='Modifier'>˜</span><span class='Gets'>↩</span> <span class='Number'>0</span><span class='Ligature'>‿</span><span class='Number'>1</span> ⟨ 0 1 2 3 4 ⟩ </pre> <p>But what about functions with only one argument? It's possible to do this using a dummy right argument such as the null character, <code><span class='String'>@</span></code>. To turn a function that takes one argument into one that takes two, we can compose it with a function that takes two arguments and returns one of them. The left one, since the variable to modify is on the left hand side. Perhaps… Left? (<code><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=ImFiY2QiIOKMveKImOKKoyAid3h5eiIKCmEg4oy94oiY4oqj4oapIEA=">↗️</a><pre> <span class='String'>"abcd"</span> <span class='Function'>⌽</span><span class='Modifier2'>∘</span><span class='Function'>⊣</span> <span class='String'>"wxyz"</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8xCiJhYmNkIiDijL3iiJjiiqMgInd4eXoiCgphIOKMveKImOKKo+KGqSBA">↗️</a><pre> <span class='String'>"abcd"</span> <span class='Function'>⌽</span><span class='Modifier2'>∘</span><span class='Function'>⊣</span> <span class='String'>"wxyz"</span> "dcba" <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> ⟨ 4 3 2 1 0 ⟩ </pre> <p>But fortunately, there's a simpler syntax as well: write your one-argument function before <code><span class='Gets'>↩</span></code> with no right hand side. Bit of a Yoda vibe: "<code><span class='Value'>a</span></code> reversed is".</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDijL3ihqkKCmEgNOKKuC3ihqkgICAgICAgICAgICMgQW5kIGJhY2sgYWdhaW4=">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>⌽</span><span class='Gets'>↩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8xCmEg4oy94oiY4oqj4oapIEAKYSDijL3ihqkKCmEgNOKKuC3ihqkgICAgICAgICAgICMgQW5kIGJhY2sgYWdhaW4=">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>⌽</span><span class='Gets'>↩</span> ⟨ 0 1 2 3 4 ⟩ <span class='Value'>a</span> <span class='Number'>4</span><span class='Modifier2'>⊸</span><span class='Function'>-</span><span class='Gets'>↩</span> <span class='Comment'># And back again </span>⟨ 4 3 2 1 0 ⟩ </pre> <p>Notice that there's no need for parentheses: modifiers bind more strongly than the assignment character. Now what if we want to decrease the last two elements of <code><span class='Value'>a</span></code>? That is, we want to compute the following array while storing it in <code><span class='Value'>a</span></code>.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=LeKfnDTijL4owq8y4oq44oaRKSBhCgphICAgICAgICAgICAgICAgICMgSXQgaGFzbid0IGNoYW5nZWQsIG9mIGNvdXJzZQ==">↗️</a><pre> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>4</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>¯2</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span><span class='Paren'>)</span> <span class='Value'>a</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8xCmEg4oy94oiY4oqj4oapIEAKYSDijL3ihqkKYSA04oq4LeKGqSAgICAgICAgICAgIyBBbmQgYmFjayBhZ2Fpbgot4p+cNOKMvijCrzLiirjihpEpIGEKCmEgICAgICAgICAgICAgICAgIyBJdCBoYXNuJ3QgY2hhbmdlZCwgb2YgY291cnNl">↗️</a><pre> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>4</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>¯2</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span><span class='Paren'>)</span> <span class='Value'>a</span> ⟨ 4 3 2 ¯3 ¯4 ⟩ <span class='Value'>a</span> <span class='Comment'># It hasn't changed, of course </span>⟨ 4 3 2 1 0 ⟩ </pre> <p>The code to do this looks the same as what we did with Reverse (<code><span class='Function'>⌽</span></code>). Again we don't have to parenthesize the function, because modifiers associate from left to right, so Under (<code><span class='Modifier2'>⌾</span></code>) binds to its operands before Compose (<code><span class='Modifier2'>∘</span></code>) does.</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSAt4p+cNOKMvijCrzLiirjihpEp4oap">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>4</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>¯2</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span><span class='Paren'>)</span><span class='Gets'>↩</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=YSDihpAgNCAgICAgICAgICAgICMgRmlyc3QgaXQncyBhIG51bWJlcgphIOKGqSA04oC/NeKAvzYgICAgICAgICMgTm93IGl0J3MgYSBsaXN0IQphIOKGqSBhIC0gMQphIC3ihqkgMQphIOKIvsuc4oapIDDigL8xCmEg4oy94oiY4oqj4oapIEAKYSDijL3ihqkKYSA04oq4LeKGqSAgICAgICAgICAgIyBBbmQgYmFjayBhZ2FpbgphIC3in5w04oy+KMKvMuKKuOKGkSnihqk=">↗️</a><pre> <span class='Value'>a</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Number'>4</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Number'>¯2</span><span class='Modifier2'>⊸</span><span class='Function'>↑</span><span class='Paren'>)</span><span class='Gets'>↩</span> ⟨ 4 3 2 ¯3 ¯4 ⟩ </pre> |
