diff options
Diffstat (limited to 'docs/doc/quick.html')
| -rw-r--r-- | docs/doc/quick.html | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/docs/doc/quick.html b/docs/doc/quick.html index 28ff1248..8a13379b 100644 --- a/docs/doc/quick.html +++ b/docs/doc/quick.html @@ -39,12 +39,13 @@ <p>If you save it with the name hello.bqn and have BQN <a href="../running.html">installed</a>, the script can be run with <code><span class='Value'>$</span> <span class='Value'>bqn</span> <span class='Value'>hello.bqn</span></code> from a shell. Because of the <code><span class='Comment'>#!</span></code> line at the top, <code><span class='Value'>$</span> <span class='Value'>.</span><span class='Function'>/</span><span class='Value'>hello.bqn</span></code> also works if <code><span class='Value'>bqn</span></code> is in your path and hello.bqn is executable. It can also be run from another BQN file in the same directory, or REPL started there, using <code><span class='Function'>•Import</span> <span class='String'>"hello.bqn"</span></code>. Or just copy-paste it into the <a href="https://mlochbaum.github.io/BQN/try.html">online REPL</a>.</p> <p>Now let's see how it works.</p> <h2 id="case-conversion"><a class="header" href="#case-conversion">Case conversion</a></h2> -<pre><span class='Comment'># Case conversion utilities -</span><span class='Value'>case</span> <span class='Gets'>←</span> <span class='Brace'>{</span> - <span class='Value'>diff</span> <span class='Gets'>←</span> <span class='Function'>-</span><span class='Modifier'>´</span> <span class='String'>"Aa"</span> - <span class='Function'>Lower</span> <span class='Gets'>⇐</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Value'>diff</span> - <span class='Function'>Upper</span> <span class='Gets'>⇐</span> <span class='Function'>Lower</span><span class='Modifier'>⁼</span> -<span class='Brace'>}</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=IyBDYXNlIGNvbnZlcnNpb24gdXRpbGl0aWVzCmNhc2Ug4oaQIHsKICBkaWZmIOKGkCAtwrQgIkFhIgogIExvd2VyIOKHkCAt4p+cZGlmZgogIFVwcGVyIOKHkCBMb3dlcuKBvAp9">↗️</a><pre> <span class='Comment'># Case conversion utilities +</span><span class='Error'>Error: Empty program</span> + <span class='Value'>case</span> <span class='Gets'>←</span> <span class='Brace'>{</span> + <span class='Value'>diff</span> <span class='Gets'>←</span> <span class='Function'>-</span><span class='Modifier'>´</span> <span class='String'>"Aa"</span> + <span class='Function'>Lower</span> <span class='Gets'>⇐</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Value'>diff</span> + <span class='Function'>Upper</span> <span class='Gets'>⇐</span> <span class='Function'>Lower</span><span class='Modifier'>⁼</span> + <span class='Brace'>}</span> </pre> <p>This part of the code defines a <a href="namespace.html">namespace</a> using braces <code><span class='Brace'>{}</span></code>, then <a href="expression.html#assignment">assigns</a> it to the name <code><span class='Value'>case</span></code>. There are three assignments inside the namespace too. Since BQN uses <a href="lexical.html">lexical scoping</a>, code outside the namespace can't access the variables <code><span class='Value'>diff</span></code>, <code><span class='Function'>Lower</span></code>, and <code><span class='Function'>Upper</span></code> directly. Oh, and the first line is a <a href="token.html#comments">comment</a>.</p> <p>The value <code><span class='Value'>diff</span></code> is the result of applying a <a href="ops.html#functions">function</a> <code><span class='Function'>-</span><span class='Modifier'>´</span></code> to the argument <code><span class='String'>"Aa"</span></code>. Function application is always written just by placing a function next to its arguments like this—a prefix application if there's one argument, infix if there are two, and that's the most arguments you can have. This doesn't limit BQN's capabilities because it's easy to pass a list as an argument. In fact, <code><span class='String'>"Aa"</span></code> is a <a href="token.html#characters-and-strings">string</a>, which means a list of characters. Characters are written with single quotes, so it's a list of <code><span class='String'>'A'</span></code> and <code><span class='String'>'a'</span></code>.</p> @@ -138,27 +139,26 @@ <pre><span class='Value'>hw</span> <span class='Value'>case.</span><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊑</span><span class='Modifier'>¨</span><span class='Paren'>)</span><span class='Gets'>↩</span> </pre> <p>This statement consists of the name <code><span class='Value'>hw</span></code> just defined, a compound function, and then the new character <code><span class='Gets'>↩</span></code>. This is another form of <a href="expression.html#assignment">assignment</a>, like <code><span class='Gets'>←</span></code>, but it changes the value of an existing variable instead of defining a new one. There's also some special <code><span class='Gets'>↩</span></code> syntax here: the expression <code><span class='Value'>val</span> <span class='Function'>Fn</span><span class='Gets'>↩</span></code> is shorthand for <code><span class='Value'>val</span> <span class='Gets'>↩</span> <span class='Function'>Fn</span> <span class='Value'>val</span></code>, avoiding the need to write the name <code><span class='Value'>hw</span></code> twice (and <code><span class='Value'>val</span> <span class='Function'>Fn</span><span class='Gets'>↩</span> <span class='Value'>arg</span></code> means <code><span class='Value'>val</span> <span class='Gets'>↩</span> <span class='Value'>val</span> <span class='Function'>Fn</span> <span class='Value'>arg</span></code>, like <code><span class='Function'>+=</span></code> and so on from C). So we are modifying <code><span class='Value'>hw</span></code> by applying this function <code><span class='Value'>case.</span><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊑</span><span class='Modifier'>¨</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=aHcg4oaQIDzLmCAy4oC/4oiYIOKliiAiaGVsbG93b3JsZCIKVXBwZXIg4oaQIC3in5woLcK0IkFhIinigbwKClVwcGVy4oy+KOKKkcKoKSBodwoKaHcgVXBwZXLijL4o4oqRwqgp4oapICAjIFNldHMgbmV3IHZhbHVlIGZvciBodw==">↗️</a><pre> <span class='Value'>hw</span> <span class='Gets'>←</span> <span class='Function'><</span><span class='Modifier'>˘</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Modifier2'>∘</span> <span class='Function'>⥊</span> <span class='String'>"helloworld"</span> - <span class='Function'>Upper</span> <span class='Gets'>←</span> <span class='Function'>-</span><span class='Modifier2'>⟜</span><span class='Paren'>(</span><span class='Function'>-</span><span class='Modifier'>´</span><span class='String'>"Aa"</span><span class='Paren'>)</span><span class='Modifier'>⁼</span> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=aHcg4oaQIDzLmCAy4oC/4oiYIOKliiAiaGVsbG93b3JsZCIKCmNhc2UuVXBwZXLijL4o4oqRwqgpIGh3CgpodyBjYXNlLlVwcGVy4oy+KOKKkcKoKeKGqSAgIyBTZXRzIG5ldyB2YWx1ZSBmb3IgaHc=">↗️</a><pre> <span class='Value'>hw</span> <span class='Gets'>←</span> <span class='Function'><</span><span class='Modifier'>˘</span> <span class='Number'>2</span><span class='Ligature'>‿</span><span class='Modifier2'>∘</span> <span class='Function'>⥊</span> <span class='String'>"helloworld"</span> - <span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊑</span><span class='Modifier'>¨</span><span class='Paren'>)</span> <span class='Value'>hw</span> + <span class='Value'>case.</span><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊑</span><span class='Modifier'>¨</span><span class='Paren'>)</span> <span class='Value'>hw</span> ⟨ "Hello" "World" ⟩ - <span class='Value'>hw</span> <span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊑</span><span class='Modifier'>¨</span><span class='Paren'>)</span><span class='Gets'>↩</span> <span class='Comment'># Sets new value for hw + <span class='Value'>hw</span> <span class='Value'>case.</span><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Paren'>(</span><span class='Function'>⊑</span><span class='Modifier'>¨</span><span class='Paren'>)</span><span class='Gets'>↩</span> <span class='Comment'># Sets new value for hw </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=4oqRwqggaHcKClVwcGVyICJodyI=">↗️</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=4oqRwqggaHcKCmNhc2UuVXBwZXIgImh3Ig==">↗️</a><pre> <span class='Function'>⊑</span><span class='Modifier'>¨</span> <span class='Value'>hw</span> "HW" - <span class='Function'>Upper</span> <span class='String'>"hw"</span> + <span class='Value'>case.</span><span class='Function'>Upper</span> <span class='String'>"hw"</span> "HW" </pre> -<p>The Under modifier keeps track of where that string came from and puts it <em>back</em>, to produce a new, altered array. It's kind of special, like Undo, but works on all sorts of fancy selections. It's also worth pointing out that <code><span class='Function'>Upper</span></code> applies to a string here, not an individual character. That's because arithmetic is <a href="arithmetic.html#pervasion">pervasive</a>, so that functions made of arithmetic naturally work on arrays. Although in this case it wasn't really necessary, because it's also possible to map over the two strings and uppercase the first character of each separately:</p> -<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=VXBwZXLijL7iipHCqCAiaGVsbG8i4oC/IndvcmxkIg==">↗️</a><pre> <span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Function'>⊑</span><span class='Modifier'>¨</span> <span class='String'>"hello"</span><span class='Ligature'>‿</span><span class='String'>"world"</span> +<p>The Under modifier keeps track of where that string came from and puts it <em>back</em>, to produce a new, altered array. It's kind of special, like Undo, but works on all sorts of fancy selections. It's also worth pointing out that <code><span class='Value'>case.</span><span class='Function'>Upper</span></code> applies to a string here, not an individual character. That's because arithmetic is <a href="arithmetic.html#pervasion">pervasive</a>, so that functions made of arithmetic naturally work on arrays. Although in this case it wasn't really necessary, because it's also possible to map over the two strings and uppercase the first character of each separately:</p> +<a class="replLink" title="Open in the REPL" target="_blank" href="https://mlochbaum.github.io/BQN/try.html#code=Y2FzZS5VcHBlcuKMvuKKkcKoICJoZWxsbyLigL8id29ybGQi">↗️</a><pre> <span class='Value'>case.</span><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Function'>⊑</span><span class='Modifier'>¨</span> <span class='String'>"hello"</span><span class='Ligature'>‿</span><span class='String'>"world"</span> ⟨ "Hello" "World" ⟩ </pre> -<p>Modifiers are applied from left to right, opposite to functions (1-modifiers also take the operand on the left while prefix functions have the argument on the right). So <code><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Function'>⊑</span><span class='Modifier'>¨</span></code> means <code><span class='Paren'>(</span><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Function'>⊑</span><span class='Paren'>)</span><span class='Modifier'>¨</span></code>.</p> +<p>Modifiers are applied from left to right, opposite to functions (1-modifiers also take the operand on the left while prefix functions have the argument on the right). So <code><span class='Value'>case.</span><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Function'>⊑</span><span class='Modifier'>¨</span></code> means <code><span class='Paren'>(</span><span class='Value'>case.</span><span class='Function'>Upper</span><span class='Modifier2'>⌾</span><span class='Function'>⊑</span><span class='Paren'>)</span><span class='Modifier'>¨</span></code>.</p> <h3 id="punctuation-and-printing"><a class="header" href="#punctuation-and-printing">Punctuation and printing</a></h3> <p>The variable <code><span class='Value'>hw</span></code> is modified one more time, then printed, producing the output Hello, World!</p> <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! |
