Update interactive example

This commit is contained in:
Richard Feldman 2023-10-28 21:45:03 -04:00
parent 67fa06f1b2
commit 0ec3f799dd
No known key found for this signature in database
GPG Key ID: F1F21AA5B1D9E43B
3 changed files with 10 additions and 10 deletions

View File

@ -28,29 +28,29 @@ view =
sectionsToStr [
Desc [Comment "<span class='desktop'>Click anything here to see an explanation.</span><span class='mobile'>Tap anything here to\n# see an explanation.</span>"] "<p><a href=\"/tutorial#comments\">Comments</a> in Roc begin with a <code>#</code> and go to the end of the line.</p>",
Newline,
Desc [Ident "main", Kw "="] "<p>This begins the definition of <code class=\"ident\">main</code>, which is where our program will begin.</p><p>In Roc, <a href=\"/tutorial#https://www.roc-lang.org/tutorial#naming-things\">assignments are always constant</a>, which means writing <code class=\"ident\">main =</code> again in the same scope would give an error.</p>",
Desc [Ident "main", Kw "="] "<p>This defines <code class=\"ident\">main</code>, which is where our program will begin.</p><p>In Roc, <a href=\"/tutorial#https://www.roc-lang.org/tutorial#naming-things\">all definitions are constant</a>, so writing <code class=\"ident\">main =</code> again in the same scope would give an error.</p>",
Indent,
Desc [Ident "Path.fromStr", Str "\"url.txt\""] "<p>This converts the string <code>\"url.txt\"</code> into a <code>Path</code> by passing it to the <code>Path.fromStr</code> function.</p><p>Function arguments are separated with whitespace rather than commas. Parens are only needed in <a href=\"/tutorial#calling-functions\">nested function calls</a>.</p>",
Desc [Ident "Path.fromStr", Str "\"url.txt\""] "<p>This converts the string <code>\"url.txt\"</code> into a <code>Path</code> by passing it to <code>Path.fromStr</code>.</p><p>Function arguments are separated with whitespace. Parentheses are only needed in <a href=\"/tutorial#calling-functions\">nested function calls</a>.</p>",
Newline,
Desc [Kw "|>", Ident "storeEmail"] "<p>The <a href=\"/tutorial#the-pipe-operator\">pipe operator</a> (<code>|></code>) is syntax sugar for passing the previous answer to the next function in the “pipeline.”</p><p>So far this pipeline desugars to:</p><pre><code>storeEmail\n (Path.fromStr \"url.txt\")</code></pre><p>The next <code>|></code> continues the pipeline.</p>",
Desc [Kw "|>", Ident "storeEmail"] "<p>The <a href=\"/tutorial#the-pipe-operator\">pipe operator</a> (<code>|></code>) is syntax sugar for passing the previous value to the next function in the “pipeline.”</p><p>Here, we're taking the value returned by <code>Path.fromStr \"url.txt\"</code> and passing it to <code>storeEmail</code>.</p><p>The next <code>|></code> continues the pipeline.</p>",
Newline,
Desc [Kw "|>", Ident "Task.onErr", Ident "handleErr"] "<p>If the task returned by the previous step in the pipeline fails, pass its error to <code>handleErr</code>.</p><p>The whole pipeline now desugars to:</p><pre><code>Task.onErr\n (storeEmail …)\n handleErr</code></pre><p>It creates a <code>Path</code> from a string, then stores an email based on that path, then handles any errors that happened.</p>",
Desc [Kw "|>", Ident "Task.onErr", Ident "handleErr"] "<p>If the task returned by the previous step in the pipeline fails, pass its error to <code>handleErr</code>.</p><p>The pipeline essentially does this:</p><pre><code>val1 = Path.fromStr \"url.txt\"\nval2 = storeEmail val1\n\nTask.onErr val2 handleErr</code></pre><p>It creates a <code>Path</code> from a string, stores an email based on that path, and then does error handling.</p>",
Outdent,
Newline,
Desc [Ident "storeEmail", Kw "=", Lambda ["filename"]] "<p>This <a href=\"/tutorial#defining-functions\">defines a function</a> named <code>storeEmail</code>.</p><p>In Roc, functions are ordinary values, so we assign names to them using <code>=</code> like with any other value.</p><p>The <code>\\arg1, arg2 -&gt;</code> syntax begins a function, and the part after <code>-&gt;</code> is the function's body.</p>",
Indent,
Desc [Ident "url", Kw "<-", Ident "File.readUtf8", Ident "filename", Kw "|>", Ident "Task.await"] "<p>This reads the contents of the file (as a <a href=\"https://en.wikipedia.org/wiki/UTF-8\">UTF-8</a> string) into <code>url</code>.</p><p>The <code>&lt;-</code> operator does <a href=\"/tutorial#backpassing\">backpassing</a>, which is syntax sugar for defining a function. This whole line desugars to:</p><pre><code>Task.await\n (File.readUtf8 filename)\n \\url -&gt;</code></pre><p>The lines after this one form the body of the <code>\\url -&gt;</code> <a href=\"https://en.wikipedia.org/wiki/Callback_(computer_programming)\">callback</a> function, which runs if the file read succeeds.</p>",
Desc [Ident "url", Kw "<-", Ident "File.readUtf8", Ident "filename", Kw "|>", Ident "Task.await"] "<p>This reads the contents of the file (as a <a href=\"https://en.wikipedia.org/wiki/UTF-8\">UTF-8</a> string) into <code>url</code>.</p><p>The <code>&lt;-</code> does <a href=\"/tutorial#backpassing\">backpassing</a>, which is syntax sugar for defining a function. This whole line desugars to:</p><pre><code>Task.await\n (File.readUtf8 filename)\n \\url -&gt;</code></pre><p>The lines after this one form the body of the <code>\\url -&gt;</code> <a href=\"https://en.wikipedia.org/wiki/Callback_(computer_programming)\">callback</a>, which runs if the file read succeeds.</p>",
Newline,
Desc [Literal "{", Ident "name", Literal ",", Ident "email", Literal "}"] "<p>This is <a href=\"/tutorial#record-destructuring\">record destructuring</a> syntax.</p><p>It takes ",
Desc [Kw "<-", Ident "Http.get", Ident "url", Ident "Json.codec"] "<p>TODO Json.codec, type inference, early error</p>",
Desc [Kw "|>", Ident "Task.await"] "<p>TODO Task.await</p>",
Newline,
Desc [Ident "dest", Kw "=", Str "\"\\(name).txt\""] "<p>The <code>\\(name)</code> will be replaced in the string with the contents of <code>name</code>. This is called <a href=\"/tutorial#string-interpolation\">string interpolation</a>.</p><p>Note that there's no <code>|> Task.await</code> on this line. Earlier lines needed that because they were I/O <a href=\"/tutorial#tasks\">tasks</a>, but this line is an ordinary assignment, so there's no task to await.</p>",
Desc [Ident "dest", Kw "=", Str "\"\\(name).txt\""] "<p>The <code>\\(name)</code> in this string literal will be replaced with the value in <code>name</code>. This is <a href=\"/tutorial#string-interpolation\">string interpolation</a>.</p><p>Note that this line doesn't end with <code>|> Task.await</code>. Earlier lines needed that because they were I/O <a href=\"/tutorial#tasks\">tasks</a>, but this is a plain old <a href=\"/tutorial#defs\">definition</a>, so there's no task to await.</p>",
Newline,
Desc [Literal "_"] "<p>In Roc, if you dont want to bother naming something, you can always choose the name <code>_</code>.</p><p>You can name as many things as you like <code>_</code>, even in the same scope, but you cant reference anything named <code>_</code>.</p><p>So its only useful for when you dont want to choose a name.</p>",
Desc [Kw "<-", Ident "File.writeUtf8", Ident "(Path.fromStr dest)", Ident "email", Kw "|>", Ident "Task.await"] "<p>This writes the <code>email</code> string to the file encoded as <a href=\"https://en.wikipedia.org/wiki/UTF-8\">UTF-8</a>.</p><p>The parentheses here show where the nested call to <code>Path.fromStr</code> begins and ends.</p>",
Newline,
Desc [Ident "Stdout.line", Str "\"Wrote email to \\(dest)\""] "<p>Note that theres no <code>|> Task.await</code> here, like there were after earlier tasks.</p><p>Thats because, although <code>Stdout.line</code> does return a <a href=\"/tutorial#tasks\">task</a>, we dont need to await it because nothing else happens after it.</p>",
Desc [Ident "Stdout.line", Str "\"Wrote email to \\(dest)\""] "<p>This prints what we did to <a href=\"https://en.wikipedia.org/wiki/Standard_streams#Standard_output_(stdout)\">stdout</a>.</p><p>Note that this line doesn't end with <code>|> Task.await</code>. Thats because, although <code>Stdout.line</code> returns a <a href=\"/tutorial#tasks\">task</a>, we dont need to await it because nothing happens after it.</p>",
Outdent,
Newline,
Desc [Ident "handleErr", Kw "=", Lambda ["err"]] "<p>Like <code>storeEmail</code>, <code>handleErr</code> is also a function.</p><p>Although type annotations are optional everywhere in Roc—because the language has 100% type inference—you could add type annotations to <code>main</code>, <code>storeEmail</code>, and <code>handleErr</code> if you wanted to.</p>",

View File

@ -70,7 +70,7 @@ You can try out Roc right now using this read-eval-print loop (REPL), which runs
## Use Cases
Roc is a very new language (it doesnt even have a numbered release yet, just nightly builds!) but it can already be used for several things if youre up for being an early adopter—with all the bugs and missing features which come with that territory.
Roc is a very young language (it doesnt even have a numbered release yet, just nightly builds!) but it can already be used for several things if youre up for being an early adopter—with all the bugs and missing features which come with that territory.
Currently these use cases are the best-supported:
@ -89,7 +89,7 @@ You can create your own! Learn about **platforms and applications**...
## Larger Example
Heres a larger example that shows a few different aspects of Roc:
* File I/O and HTTP
* File I/O and HTTP requests
* Pattern matching for error handling
* JSON deserialization via type inference
* Common syntax sugar: string interpolation, pipelines, and backpassing

View File

@ -813,7 +813,7 @@ code .dim {
.interactive-desc {
display: none;
background-color: #ddd;
padding: 8px 16px;
padding: 0 16px;
margin-top: 9px;
}