aboutsummaryrefslogtreecommitdiff
path: root/docs/doc/choose.html
blob: d76da055a6bc2c4eae5dd064e7e7a5261cbfd6ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<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 the selecting index returned by a function <code><span class='Function'>๐”ฝ</span></code>. 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 its 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 &quot;false&quot; comes first in <code><span class='Value'>๐•˜</span></code> and &quot;true&quot; comes second, which is backwards relative to if-else constructs in most programming languages (including BQN's own <a href="block.html#predicates">predicates</a>). 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'>&lt;</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 answer 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'>&gt;</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=Rm4g4oaQICjiipEicnRkIuKKkOKKjynil7bin6jijL0sIDHiirjihpEsIDHiirjihpMsIOKKouKfqSAgIyBSZXZlcnNlLCB0YWtlIDEsIGRyb3AgMQoKRm4gInIxMjMiCgpGbiAiZDEyMyIKCkZuICIxMjMiICAjIERlZmF1bHQ=">โ†—๏ธ</a><pre>    <span class='Function'>Fn</span> <span class='Gets'>โ†</span> <span class='Paren'>(</span><span class='Function'>โŠ‘</span><span class='String'>&quot;rtd&quot;</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'>&quot;r123&quot;</span>
"321r"

    <span class='Function'>Fn</span> <span class='String'>&quot;d123&quot;</span>
"123"

    <span class='Function'>Fn</span> <span class='String'>&quot;123&quot;</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'>&quot;rtd&quot;</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>