diff options
| author | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-09-26 21:48:54 -0400 |
|---|---|---|
| committer | Marshall Lochbaum <mwlochbaum@gmail.com> | 2021-09-26 21:48:54 -0400 |
| commit | 84bf304c69c8fd662d2ee0d4770fc67981e4b222 (patch) | |
| tree | cd44118907df0c8ecd9524418d96cf6ff424e9b0 /docs | |
| parent | 008894473a5d2177e5683719b718e0acefd515bc (diff) | |
Use monadic modified assignment in the tutorial
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/tutorial/variable.html | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/docs/tutorial/variable.html b/docs/tutorial/variable.html index e8a1f06e..226b54ae 100644 --- a/docs/tutorial/variable.html +++ b/docs/tutorial/variable.html @@ -304,13 +304,20 @@ ERROR <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> ⟨ 0 1 2 3 4 ⟩ </pre> -<p>But what about functions with only one argument? The syntax isn't the best, but at least it's possible. There still needs to be a value on the right side of the assignment even if it'll be ignored; I use the null character <code><span class='String'>@</span></code> for this. Then 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> +<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> "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> +⟨ 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> ⟨ 4 3 2 ¯3 ¯4 ⟩ @@ -319,6 +326,6 @@ ERROR </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+cNOKMvijCrzLiirjihpEp4oiY4oqj4oapIEA=">↗️</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='Modifier2'>∘</span><span class='Function'>⊣</span><span class='Gets'>↩</span> <span class='String'>@</span> +<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> ⟨ 4 3 2 ¯3 ¯4 ⟩ </pre> |
