Skip to content

Commit 99b145b

Browse files
update tuples example
1 parent 1d180e9 commit 99b145b

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

examples/Tuples/README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ For example, instead of having `foo.name` to access the `name` field of a record
55
you might write `foo.0` to access the first field in a tuple (or `foo.1` for the second
66
field, etc.)
77

8-
98
## Code
9+
1010
```roc
1111
file:main.roc
1212
```
@@ -17,8 +17,9 @@ Run this from the directory that has `main.roc` in it:
1717

1818
```
1919
$ roc main.roc
20-
First is: A String,
20+
First is: Just a String,
2121
Second is: true,
22-
Third is: 15000000.
22+
Third is: 15000000.0.
23+
2324
You also have some pears.
2425
```

examples/Tuples/main.roc

Lines changed: 38 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
app [main!] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.20.0/X73hGh05nNTkDHU06FHC0YfFaQB1pimX7gncRcao5mU.tar.br" }
2-
3-
import pf.Stdout
4-
import pf.Arg exposing [Arg]
5-
6-
main! : List Arg => Result {} _
7-
main! = |_args|
8-
9-
# a tuple that contains three different types
10-
simple_tuple : (Str, Bool, I64)
11-
simple_tuple = ("A String", Bool.true, 15_000_000)
12-
13-
# access the items in a tuple by index (starts at 0)
14-
first_item = simple_tuple.0
15-
second_item = if simple_tuple.1 then "true" else "false"
16-
third_item = Num.to_str(simple_tuple.2)
17-
18-
Stdout.line!(
19-
"""
20-
First is: ${first_item},
21-
Second is: ${second_item},
22-
Third is: ${third_item}.
23-
""",
24-
)?
25-
26-
# You can also use tuples with `when`:
27-
fruit_selection : [Apple, Pear, Banana]
28-
fruit_selection = Pear
29-
30-
quantity = 12
31-
32-
when (fruit_selection, quantity) is
33-
# TODO re-enable when github.com/roc-lang/roc/issues/5530 is fixed.
34-
# (_, qty) if qty == 0 -> Stdout.line! "You have no fruit."
35-
(Apple, _) -> Stdout.line!("You also have some apples.")
36-
(Pear, _) -> Stdout.line!("You also have some pears.")
37-
(Banana, _) -> Stdout.line!("You also have some bananas.")
1+
main! : List(Str) => Try({}, _)
2+
main! = |_args| {
3+
4+
# A tuple that contains three different types
5+
simple_tuple : (Str, Bool, Dec)
6+
simple_tuple = ("Just a String", True, 15_000_000)
7+
8+
9+
# Access the items in a tuple by index (starts at 0)
10+
first_item = simple_tuple.0
11+
second_item = if simple_tuple.1 "true" else "false"
12+
third_item = simple_tuple.2.to_str()
13+
14+
echo!(
15+
\\First is: ${first_item},
16+
\\Second is: ${second_item},
17+
\\Third is: ${third_item}.
18+
\\
19+
)
20+
21+
# You can also use tuples with `match`:
22+
fruit_selection : [Apple, Pear, Banana]
23+
fruit_selection = Pear
24+
25+
quantity = 12
26+
27+
# Create a tuple of fruites and quantities,
28+
# So you can match on both of them
29+
match (fruit_selection, quantity) {
30+
(_, 0) => echo!("You have no fruit.")
31+
(Apple, 1) => echo!("You have an apple.")
32+
(Apple, _) => echo!("You also have some apples.")
33+
(Pear, _) => echo!("You also have some pears.")
34+
(Banana, _) => echo!("You also have some bananas.")
35+
}
36+
37+
Ok({})
38+
}

0 commit comments

Comments
 (0)