Skip to content

Commit

Permalink
Simplify & shorten the code snippet
Browse files Browse the repository at this point in the history
  • Loading branch information
ageron committed Aug 20, 2024
1 parent feb2abb commit a493200
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions docs/SNIPPET.txt
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
main =
Path.fromStr "url.txt"
|> storeEmail
|> Task.onErr handleErr
app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.12.0/Lb8EgiejTUzbggO2HVVuPJFkwvvsfW6LojkLR20kTVE.tar.br" }

import pf.Stdout

storeEmail = \path ->
url = File.readUtf8! path
user = Http.get! url Json.utf8
dest = Path.fromStr "$(user.name).txt"
File.writeUtf8! dest user.email
Stdout.line! "Wrote email to $(Path.display dest)"
factorial = \n ->
if n == 1 then
1
else
n * factorial (n - 1)

handleErr = \err ->
when err is
HttpErr url _ -> Stderr.line! "Error fetching URL $(url)"
FileReadErr path _ -> Stderr.line! "Error reading from $(Path.display path)"
FileWriteErr path _ -> Stderr.line! "Error writing to $(Path.display path)"
main =
result = factorial 5
Stdout.line! "factorial 5 = 1 * 2 * 3 * 4 * 5 = $(Num.toStr result)"

0 comments on commit a493200

Please sign in to comment.