Skip to content

Commit a46e0ce

Browse files
update examples to use ! syntax
1 parent ad6331d commit a46e0ce

File tree

17 files changed

+142
-182
lines changed

17 files changed

+142
-182
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ examples/CommandLineArgs/main
1818
examples/Tasks/main
1919
examples/Tuples/main
2020
examples/EncodeDecode/main
21+
examples/GoPlatform/platform/*.dylib
2122
roc_nightly/
2223

2324
# macOS directory attributes

examples/Arithmetic/main.roc

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
app "arithmetic"
2-
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br" }
2+
packages { pf: "../../../basic-cli/platform/main.roc" }
33
imports [
44
pf.Stdout,
55
pf.Task,
@@ -11,7 +11,7 @@ TaskErrors : [InvalidArg, InvalidNumStr]
1111

1212
main =
1313
task =
14-
args <- readArgs |> Task.await
14+
args = readArgs!
1515

1616
formatResult = \(operation, result) ->
1717
resultStr = Num.toStr result
@@ -36,15 +36,8 @@ main =
3636

3737
when taskResult is
3838
Ok result -> Stdout.line result
39-
Err InvalidArg ->
40-
{} <- Stdout.line "Error: Please provide two integers between -1000 and 1000 as arguments." |> Task.await
41-
42-
Task.err 1 # 1 is an exit code to indicate failure
43-
44-
Err InvalidNumStr ->
45-
{} <- Stdout.line "Error: Invalid number format. Please provide integers between -1000 and 1000." |> Task.await
46-
47-
Task.err 1 # 1 is an exit code to indicate failure
39+
Err InvalidArg -> Task.err (Exit 1 "Error: Please provide two integers between -1000 and 1000 as arguments.")
40+
Err InvalidNumStr -> Task.err (Exit 1 "Error: Invalid number format. Please provide integers between -1000 and 1000.")
4841

4942
## Reads two command-line arguments, attempts to parse them as `I32` numbers,
5043
## and returns a task containing a record with two fields, `a` and `b`, holding
@@ -56,17 +49,19 @@ main =
5649
## error `InvalidArg` or `InvalidNumStr`.
5750
readArgs : Task.Task { a : I32, b : I32 } TaskErrors
5851
readArgs =
59-
Arg.list
60-
|> Task.mapErr \_ -> InvalidArg
61-
|> Task.await \args ->
62-
aResult = List.get args 1 |> Result.try Str.toI32
63-
bResult = List.get args 2 |> Result.try Str.toI32
6452

65-
when (aResult, bResult) is
66-
(Ok a, Ok b) ->
67-
if a < -1000 || a > 1000 || b < -1000 || b > 1000 then
68-
Task.err InvalidNumStr
69-
else
70-
Task.ok { a, b }
53+
args =
54+
Arg.list
55+
|> Task.mapErr! \_ -> InvalidArg
56+
57+
aResult = List.get args 1 |> Result.try Str.toI32
58+
bResult = List.get args 2 |> Result.try Str.toI32
59+
60+
when (aResult, bResult) is
61+
(Ok a, Ok b) ->
62+
if a < -1000 || a > 1000 || b < -1000 || b > 1000 then
63+
Task.err InvalidNumStr
64+
else
65+
Task.ok { a, b }
7166

72-
_ -> Task.err InvalidNumStr
67+
_ -> Task.err InvalidNumStr

examples/CommandLineArgs/main.roc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,45 @@
11
# Run with `roc ./examples/CommandLineArgs/main.roc -- examples/CommandLineArgs/input.txt`
22
app "command-line-args"
33
packages {
4-
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br",
4+
pf: "../../../basic-cli/platform/main.roc",
55
}
66
imports [
77
pf.Stdout,
8-
pf.Stderr,
98
pf.File,
109
pf.Path,
1110
pf.Task,
1211
pf.Arg,
1312
]
1413
provides [main] to pf
1514

16-
main : Task.Task {} I32
1715
main =
1816
finalTask =
1917
# try to read the first command line argument
20-
pathArg <- Task.await readFirstArgT
18+
pathArg = readFirstArgT!
2119

2220
readFileToStr (Path.fromStr pathArg)
2321

2422
finalResult <- Task.attempt finalTask
2523

2624
when finalResult is
27-
Err ZeroArgsGiven ->
28-
{} <- Stderr.line "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc command-line-args.roc -- path/to/input.txt`" |> Task.await
29-
30-
Task.err 1 # 1 is an exit code to indicate failure
25+
Err ZeroArgsGiven ->
26+
27+
Task.err (Exit 1 "Error ZeroArgsGiven:\n\tI expected one argument, but I got none.\n\tRun the app like this: `roc command-line-args.roc -- path/to/input.txt`")
3128

3229
Err (ReadFileErr errMsg) ->
3330
indentedErrMsg = indentLines errMsg
34-
{} <- Stderr.line "Error ReadFileErr:\n$(indentedErrMsg)" |> Task.await
3531

36-
Task.err 1 # 1 is an exit code to indicate failure
32+
Task.err (Exit 1 "Error ReadFileErr:\n$(indentedErrMsg)")
3733

3834
Ok fileContentStr ->
35+
3936
Stdout.line "file content: $(fileContentStr)"
4037

4138
# Task to read the first CLI arg (= Str)
4239
readFirstArgT : Task.Task Str [ZeroArgsGiven]_
4340
readFirstArgT =
4441
# read all command line arguments
45-
args <- Arg.list |> Task.await
42+
args = Arg.list!
4643

4744
# get the second argument, the first is the executable's path
4845
List.get args 1 |> Result.mapErr (\_ -> ZeroArgsGiven) |> Task.fromResult

examples/EncodeDecode/main.roc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
app "example"
22
packages {
3-
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br",
3+
pf: "../../../basic-cli/platform/main.roc",
44
json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.7.0/xuaMzXRVG_SEhOFZucS3iBozlRdObWsfKaYZMHVE_q0.tar.br",
55
}
66
imports [
77
pf.Stdout.{ line },
8+
pf.Task,
89
json.Core.{ json },
910
Decode.{ Decoder, DecoderFormatting, DecodeResult, DecodeError },
1011
Encode.{ Encoder, EncoderFormatting },

examples/FizzBuzz/main.roc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
app "fizz-buzz"
2-
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br" }
3-
imports [pf.Stdout]
2+
packages { pf: "../../../basic-cli/platform/main.roc" }
3+
imports [pf.Stdout, pf.Task]
44
provides [main] to pf
55

66
main =

examples/HelloWorld/main.roc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
app "hello-world"
2-
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br" }
3-
imports [pf.Stdout]
2+
packages { pf: "../../../basic-cli/platform/main.roc" }
3+
imports [pf.Stdout, pf.Task]
44
provides [main] to pf
55

6-
main = Stdout.line "Hello, World!"
6+
main =
7+
Stdout.line! "Hello, World!"

examples/IngestFiles/main.roc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
app "ingested-file"
2-
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br" }
2+
packages { pf: "../../../basic-cli/platform/main.roc" }
33
imports [
44
pf.Stdout,
5+
pf.Task,
56
"sample.txt" as sample : Str,
67
]
78
provides [main] to pf
89

910
main =
10-
Stdout.line "$(sample)"
11+
Stdout.line! "$(sample)"

examples/Json/main.roc

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
app "json-basic"
22
packages {
3-
cli: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br",
3+
cli: "../../../basic-cli/platform/main.roc",
44
json: "https://github.com/lukewilliamboswell/roc-json/releases/download/0.7.0/xuaMzXRVG_SEhOFZucS3iBozlRdObWsfKaYZMHVE_q0.tar.br",
55
}
66
imports [
@@ -24,13 +24,8 @@ main =
2424
decoded = fromBytesPartial requestBody decoder
2525

2626
when decoded.result is
27-
Ok record ->
28-
Stdout.line "Successfully decoded image, title:\"$(record.image.title)\""
29-
30-
Err _ ->
31-
{} <- Stdout.line "Error, failed to decode image" |> Task.await
32-
33-
Task.err 1 # 1 is an exit code to indicate failure
27+
Ok record -> Stdout.line "Successfully decoded image, title:\"$(record.image.title)\""
28+
Err _ -> Task.err (Exit 1 "Error, failed to decode image")
3429

3530
ImageRequest : {
3631
image : {

examples/LeastSquares/main.roc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
app "example"
2-
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br" }
2+
packages { pf: "../../../basic-cli/platform/main.roc" }
33
imports [
44
pf.Stdout,
5+
pf.Task,
56
]
67
provides [main] to pf
78

examples/MultipleRocFiles/main.roc

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
app "interface-modules"
2-
packages { pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br" }
2+
packages { pf: "../../../basic-cli/platform/main.roc" }
33
# we import Stdout from the platform and the Hello interface from the Hello.roc file
4-
imports [pf.Stdout, Hello]
4+
imports [pf.Stdout, pf.Task, Hello]
55
provides [main] to pf
66

7-
main = Stdout.line (Hello.hello "World")
7+
main =
8+
Stdout.line! (Hello.hello "World")

examples/Parser/main.roc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
app "parser-basic"
22
packages {
3-
cli: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br",
3+
cli: "../../../basic-cli/platform/main.roc",
44
parser: "https://github.com/lukewilliamboswell/roc-parser/releases/download/0.5.2/9VrPjwfQQ1QeSL3CfmWr2Pr9DESdDIXy97pwpuq84Ck.tar.br",
55
}
66
imports [
77
cli.Stdout,
8+
cli.Task,
89
parser.Core.{ Parser, many, oneOf, map },
910
parser.String.{ parseStr, codeunit, anyCodeunit },
1011
]
@@ -16,7 +17,7 @@ main =
1617
|> Result.map countLetterAs
1718
|> Result.map \count -> "I counted $(count) letter A's!"
1819
|> Result.withDefault "Ooops, something went wrong parsing"
19-
|> Stdout.line
20+
|> Stdout.line!
2021

2122
Letter : [A, B, C, Other]
2223

examples/RandomNumbers/main.roc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
app "random-numbers"
22
packages {
3-
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br",
3+
pf: "../../../basic-cli/platform/main.roc",
44
rand: "https://github.com/lukewilliamboswell/roc-random/releases/download/0.0.1/x_XwrgehcQI4KukXligrAkWTavqDAdE5jGamURpaX-M.tar.br",
55
}
66
imports [
77
pf.Stdout,
8+
pf.Task,
89
rand.Random,
910
]
1011
provides [main] to pf
@@ -27,7 +28,7 @@ main =
2728
|> List.map Num.toStr
2829
|> Str.joinWith ","
2930

30-
Stdout.line "Random numbers are: $(numbersListStr)"
31+
Stdout.line! "Random numbers are: $(numbersListStr)"
3132

3233
# Generate a list of numbers using the seed and generator provided
3334
# This is NOT cryptograhpically secure!

examples/TaskLoop/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ I64 -> Task [Done I64, Step I64] [NotNum Str]
3232
This is where the action happens:
3333
```roc
3434
addNumberFromStdin = \sum ->
35-
input <- Stdin.line |> Task.await
35+
input = Stdin.line!
3636
3737
addResult =
3838
when input is

examples/TaskLoop/main.roc

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,34 @@
11
app "task-usage"
22
packages {
3-
pf: "https://github.com/roc-lang/basic-cli/releases/download/0.9.1/y_Ww7a2_ZGjp0ZTt9Y_pNdSqqMRdMLzHMKfdN8LWidk.tar.br",
3+
pf: "../../../basic-cli/platform/main.roc",
44
}
55
imports [pf.Stdin, pf.Stdout, pf.Stderr, pf.Task.{ Task }]
66
provides [main] to pf
77

8-
run : Task {} [NotNum Str]
9-
run =
10-
{} <- Stdout.line "Enter some numbers on different lines, then press Ctrl-D to sum them up." |> Task.await
11-
sum <- Task.loop 0 addNumberFromStdin |> Task.await
12-
13-
Stdout.line "Sum: $(Num.toStr sum)"
14-
15-
addNumberFromStdin : I64 -> Task [Done I64, Step I64] [NotNum Str]
16-
addNumberFromStdin = \sum ->
17-
input <- Stdin.line |> Task.await
18-
19-
addResult =
20-
when input is
21-
Input text ->
22-
when Str.toI64 text is
23-
Ok num ->
24-
Ok (Step (sum + num))
8+
main = run |> Task.onErr printErr
259

26-
Err InvalidNumStr ->
27-
Err (NotNum text)
28-
29-
End ->
30-
Ok (Done sum)
10+
run : Task {} _
11+
run =
12+
Stdout.line! "Enter some numbers on different lines, then press Ctrl-D to sum them up."
3113

32-
Task.fromResult addResult
14+
sum = Task.loop! 0 addNumberFromStdin
3315

34-
main =
35-
run |> Task.onErr printErr
16+
Stdout.line! "Sum: $(Num.toStr sum)"
3617

37-
printErr : [NotNum Str] -> Task {} *
18+
addNumberFromStdin : I64 -> Task [Done I64, Step I64] _
19+
addNumberFromStdin = \sum ->
20+
when Stdin.line |> Task.result! is
21+
Ok input ->
22+
when Str.toI64 input is
23+
Ok num -> Task.ok (Step (sum + num))
24+
Err _ -> Task.err (NotNum input)
25+
Err (StdinErr EndOfFile) -> Task.ok (Done sum)
26+
Err err -> err |> Inspect.toStr |> NotNum |> Task.err
27+
28+
printErr : _ -> Task {} _
3829
printErr = \err ->
39-
errorMsg =
40-
when err is
41-
NotNum text -> "\"$(text)\" is not a valid I64 number."
42-
43-
Stderr.line "Error: $(errorMsg)"
30+
when err is
31+
NotNum text -> Stderr.line "Error: \"$(text)\" is not a valid I64 number."
32+
_ -> Stderr.line "Error: $(Inspect.toStr err)"
33+
34+

0 commit comments

Comments
 (0)