diff options
Diffstat (limited to 'docs/doc')
| -rw-r--r-- | docs/doc/choose.html | 30 | ||||
| -rw-r--r-- | docs/doc/index.html | 1 | ||||
| -rw-r--r-- | docs/doc/primitive.html | 2 |
3 files changed, 32 insertions, 1 deletions
diff --git a/docs/doc/choose.html b/docs/doc/choose.html new file mode 100644 index 00000000..2bcaa223 --- /dev/null +++ b/docs/doc/choose.html @@ -0,0 +1,30 @@ +<head> + <link href="../favicon.ico" rel="shortcut icon" type="image/x-icon"/> + <link href="../style.css" rel="stylesheet"/> + <title>BQN: Choose</title> +</head> +<div class="nav">(<a href="https://github.com/mlochbaum/BQN">github</a>) / <a href="../index.html">BQN</a> / <a href="index.html">doc</a></div> +<h1 id="choose"><a class="header" href="#choose">Choose</a></h1> +<p>The 2-modifier Choose (<code><span class='Modifier2'>โถ</span></code>) applies one function from a list <code><span class='Value'>๐</span></code>, based on a selection function <code><span class='Function'>๐ฝ</span></code> that returns an index. It's a combinator form of <a href="pick.html">Pick</a> (<code><span class='Function'>โ</span></code>), so that <code><span class='Brace'>{</span><span class='Value'>f</span><span class='Gets'>โ</span><span class='Paren'>(</span><span class='Value'>๐จ</span><span class='Function'>๐ฝ</span><span class='Value'>๐ฉ</span><span class='Paren'>)</span><span class='Function'>โ</span><span class='Value'>๐</span> <span class='Separator'>โ</span> <span class='Value'>๐จ</span><span class='Function'>F</span><span class='Value'>๐ฉ</span><span class='Brace'>}</span></code> is a complete definition. For example, the function below subtracts 1 from an argument if negative and adds 1 if positive.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MOKKuOKJpOKXtuKfqC3in5wxLCAr4p+cMeKfqcKoIDPigL/CrzHigL81">โ๏ธ</a><pre> <span class='Number'>0</span><span class='Modifier2'>โธ</span><span class='Function'>โค</span><span class='Modifier2'>โถ</span><span class='Bracket'>โจ</span><span class='Function'>-</span><span class='Modifier2'>โ</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Function'>+</span><span class='Modifier2'>โ</span><span class='Number'>1</span><span class='Bracket'>โฉ</span><span class='Modifier'>ยจ</span> <span class='Number'>3</span><span class='Ligature'>โฟ</span><span class='Number'>ยฏ1</span><span class='Ligature'>โฟ</span><span class='Number'>5</span> +โจ 4 ยฏ2 6 โฉ +</pre> +<p>Here the selection function <code><span class='Function'>๐ฝ</span></code> is <code><span class='Number'>0</span><span class='Modifier2'>โธ</span><span class='Function'>โค</span></code>, while <code><span class='Value'>๐</span></code> is a list of two functions <code><span class='Bracket'>โจ</span><span class='Function'>-</span><span class='Modifier2'>โ</span><span class='Number'>1</span><span class='Separator'>,</span> <span class='Function'>+</span><span class='Modifier2'>โ</span><span class='Number'>1</span><span class='Bracket'>โฉ</span></code>. On the first argument, <code><span class='Number'>3</span></code>, <code><span class='Function'>๐ฝ</span><span class='Number'>3</span></code> is <code><span class='Number'>0</span><span class='Function'>โค</span><span class='Number'>3</span></code>, or <code><span class='Number'>1</span></code>, so the function <code><span class='Function'>+</span><span class='Modifier2'>โ</span><span class='Number'>1</span></code> from <code><span class='Value'>๐</span></code> is chosen. The use of array indices means "false" comes first in <code><span class='Value'>๐</span></code> and "true" comes second, which is backwards relative to if-else constructs in most programming languages (including BQN's own predicates). When using a comparison for <code><span class='Function'>๐ฝ</span></code> I strongly prefer to phrase it as <code><span class='Value'>n</span><span class='Modifier2'>โธ</span><span class='Function'><</span></code> or <code><span class='Value'>n</span><span class='Modifier2'>โธ</span><span class='Function'>โค</span></code> so that smaller values go through the first one and larger functions go through the second. This doesn't apply so much when comparing two arguments since one is smaller but the other's larger, so I don't have an easy technique for that.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=MiA+4pe24oqj4oC/4oqiIDYgICMgQSBtaW5pbXVtIGZ1bmN0aW9uICjijIop">โ๏ธ</a><pre> <span class='Number'>2</span> <span class='Function'>></span><span class='Modifier2'>โถ</span><span class='Function'>โฃ</span><span class='Ligature'>โฟ</span><span class='Function'>โข</span> <span class='Number'>6</span> <span class='Comment'># A minimum function (โ) +</span>2 +</pre> +<p>The advantage of using an index is that Choose works with any number of options.</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Rm4g4oaQICjiipEicnRkIuKKkuKKjynil7bin6jijL0sIDHiirjihpEsIDHiirjihpMsIOKKouKfqSAgIyBSZXZlcnNlLCB0YWtlIDEsIGRyb3AgMQoKRm4gInIxMjMiCgpGbiAiZDEyMyIKCkZuICIxMjMiICAjIERlZmF1bHQ=">โ๏ธ</a><pre> <span class='Function'>Fn</span> <span class='Gets'>โ</span> <span class='Paren'>(</span><span class='Function'>โ</span><span class='String'>"rtd"</span><span class='Function'>โโ</span><span class='Paren'>)</span><span class='Modifier2'>โถ</span><span class='Bracket'>โจ</span><span class='Function'>โฝ</span><span class='Separator'>,</span> <span class='Number'>1</span><span class='Modifier2'>โธ</span><span class='Function'>โ</span><span class='Separator'>,</span> <span class='Number'>1</span><span class='Modifier2'>โธ</span><span class='Function'>โ</span><span class='Separator'>,</span> <span class='Function'>โข</span><span class='Bracket'>โฉ</span> <span class='Comment'># Reverse, take 1, drop 1 +</span> + <span class='Function'>Fn</span> <span class='String'>"r123"</span> +"321r" + + <span class='Function'>Fn</span> <span class='String'>"d123"</span> +"123" + + <span class='Function'>Fn</span> <span class='String'>"123"</span> <span class='Comment'># Default +</span>"123" +</pre> +<p>The selection function in <code><span class='Function'>Fn</span></code> uses <a href="search.html#index-of">Index of</a> (<code><span class='Function'>โ</span></code>) to find the index of the first character in the list <code><span class='String'>"rtd"</span></code>. An extra value in <code><span class='Value'>๐</span></code> serves as a default function if it's none of those, since the result of <code><span class='Function'>๐ฝ</span></code> is <code><span class='Number'>3</span></code> in that case. A similar function that's often useful is <a href="order.html#bins">Bins</a>, for grouping inputs into intervals rather than by exact matching.</p> +<p>Choose is necessary for <a href="tacit.html">tacit</a> programming, but tacit programming is not necessary to be an effective BQN programmer! Consider using block features like <a href="block.html#predicates">predicates</a> when Choose isn't working with your program's flow.</p> +<p>Because Choose is based on <a href="pick.html">Pick</a>, it retains the features of negative, multidimensional, and multiple selection. Negative indexing might make sense if there's some special <code><span class='Number'>ยฏ1</span></code> value, and if the options naturally form an array, multidimensional indexing is pretty neat. Selecting multiple values from <code><span class='Value'>๐</span></code>, which happens if the result of <code><span class='Function'>๐ฝ</span></code> is an array of arrays, is never useful because the array result from <code><span class='Function'>๐ฝ</span></code> acts as a constant function. It's much clearer to express it as <code><span class='Function'>๐ฝโ</span><span class='Value'>๐</span><span class='Modifier'>ห</span></code>.</p> diff --git a/docs/doc/index.html b/docs/doc/index.html index c3d6ea2b..66bdbfec 100644 --- a/docs/doc/index.html +++ b/docs/doc/index.html @@ -48,6 +48,7 @@ <li><a href="shape.html">Array dimensions</a> (<code><span class='Function'>โข=โ </span></code>)</li> <li><a href="assert.html">Assert and Catch</a> (<code><span class='Function'>!</span></code> and <code><span class='Modifier2'>โ</span></code>)</li> <li><a href="compose.html">Atop and Over</a> (<code><span class='Modifier2'>โโ</span></code>)</li> +<li><a href="choose.html">Choose</a> (<code><span class='Modifier2'>โถ</span></code>)</li> <li><a href="constant.html">Constant</a> (<code><span class='Modifier'>ห</span></code>)</li> <li><a href="reshape.html">Deshape and Reshape</a> (<code><span class='Function'>โฅ</span></code>)</li> <li><a href="enclose.html">Enclose</a> (<code><span class='Function'><</span></code>)</li> diff --git a/docs/doc/primitive.html b/docs/doc/primitive.html index db9140b6..aa3fd087 100644 --- a/docs/doc/primitive.html +++ b/docs/doc/primitive.html @@ -473,7 +473,7 @@ </tr> <tr> <td><code><span class='Modifier2'>โถ</span></code></td> -<td>Choose</td> +<td><a href="choose.html">Choose</a></td> <td><code><span class='Brace'>{</span><span class='Value'>f</span><span class='Gets'>โ</span><span class='Paren'>(</span><span class='Value'>๐จ</span><span class='Function'>๐ฝ</span><span class='Value'>๐ฉ</span><span class='Paren'>)</span><span class='Function'>โ</span><span class='Value'>๐</span> <span class='Separator'>โ</span> <span class='Value'>๐จ</span><span class='Function'>F</span><span class='Value'>๐ฉ</span><span class='Brace'>}</span></code></td> <td>Select one of the functions in list <code><span class='Value'>๐</span></code> based on <code><span class='Function'>๐ฝ</span></code></td> </tr> |
