Skip to content

Commit f48645d

Browse files
authored
Merge pull request #183 from bairymr/rb/list-in-roc
Example for List as usage (#159)
2 parents acfa4eb + 8a81e76 commit f48645d

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

examples/PatternMatching/PatternMatching.roc

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,44 +34,56 @@ expect
3434
expect
3535
match = \input ->
3636
when input is
37-
[.., 42] -> EndWith42
37+
[.., 42] -> EndsWith42
3838
_ -> Other
3939

40-
(match [24, 64, 42] == EndWith42)
41-
&& (match [42, 1, 5] != EndWith42)
40+
(match [24, 64, 42] == EndsWith42)
41+
&& (match [42, 1, 5] != EndsWith42)
4242

4343
# Match a list that starts with a Foo tag
4444
# followed by a Bar tag
4545
expect
4646
match = \input ->
4747
when input is
48-
[Foo, Bar, ..] -> FooBar
48+
[Foo, Bar, ..] -> StartsWithFooBar
4949
_ -> Other
5050

51-
(match [Foo, Bar, Bar] == FooBar)
52-
&& (match [Bar, Bar, Foo] != FooBar)
51+
(match [Foo, Bar, Bar] == StartsWithFooBar)
52+
&& (match [Bar, Bar, Foo] != StartsWithFooBar)
5353

5454
# Match a list with these exact elements:
5555
# Foo, Bar, and then (Baz "Hi")
5656
expect
5757
match = \input ->
5858
when input is
59-
[Foo, Bar, Baz "Hi"] -> Bingo
59+
[Foo, Bar, Baz "Hi"] -> FooBarBazStr
6060
_ -> Other
6161

62-
(match [Foo, Bar, Baz "Hi"] == Bingo)
63-
&& (match [Foo, Bar] != Bingo)
64-
&& (match [Foo, Bar, Baz "Hi", Blah] != Bingo)
62+
(match [Foo, Bar, Baz "Hi"] == FooBarBazStr)
63+
&& (match [Foo, Bar] != FooBarBazStr)
64+
&& (match [Foo, Bar, Baz "Hi", Blah] != FooBarBazStr)
6565

6666
# Match a list with Foo as its first element, and
6767
# Count for its second element. Count holds a number,
6868
# and we only match if that number is greater than 0.
6969
expect
7070
match = \input ->
7171
when input is
72-
[Foo, Count num, ..] if num > 0 -> FooBar
72+
[Foo, Count num, ..] if num > 0 -> FooCountIf
7373
_ -> Other
7474

75-
(match [Foo, Count 1] == FooBar)
76-
&& (match [Foo, Count 0] != FooBar)
77-
&& (match [Baz, Count 1] != FooBar)
75+
(match [Foo, Count 1] == FooCountIf)
76+
&& (match [Foo, Count 0] != FooCountIf)
77+
&& (match [Baz, Count 1] != FooCountIf)
78+
79+
# Use `as` to create a variable equal to the part of the list that matches `..`
80+
expect
81+
match = \input ->
82+
when input is
83+
[head, .. as tail] -> HeadAndTail head tail
84+
_ -> Other
85+
86+
(match [1, 2, 3] == HeadAndTail 1 [2, 3])
87+
&& (match [1, 2] == HeadAndTail 1 [2])
88+
&& (match [1] == HeadAndTail 1 [])
89+
&& (match [] == Other)

examples/PatternMatching/README.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,28 @@
1-
# Pattern Matching
1+
# Pattern Matching on Lists
22

3-
Some different ways to do pattern matching in Roc.
3+
All the ways to pattern match on lists:
4+
```roc
5+
when input is
6+
[] -> EmptyList
7+
8+
[_, ..] -> NonEmptyList
9+
10+
["Hi", ..] -> StartsWithHi
11+
12+
[.., 42] -> EndsWith42
13+
14+
[Foo, Bar, ..] -> StartsWithFooBar
15+
16+
[Foo, Bar, Baz "Hi"] -> FooBarBazStr
17+
18+
[Foo, Count num, ..] if num > 0 -> FooCountIf
19+
20+
[head, .. as tail] -> HeadAndTail head tail
21+
22+
_ -> Other
23+
```
24+
Note that this specific snippet would not typecheck because it uses lists of different types.
25+
This is just meant to be a compact overview. See the code section below for valid Roc.
426

527
## Code
628
```roc
@@ -14,5 +36,5 @@ Run this from the directory that has `PatternMatching.roc` in it:
1436
```
1537
$ roc test PatternMatching.roc
1638
17-
0 failed and 7 passed in 629 ms.
39+
0 failed and 8 passed in 88 ms.
1840
```

examples/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ You can find the source code for all of these at [github.com/roc-lang/examples](
88
- [FizzBuzz](/FizzBuzz/README.html)
99
- [Basic Dict Usage](/BasicDict/README.html)
1010
- [Tuples](/Tuples/README.html)
11-
- [Pattern Matching](/PatternMatching/README.html)
11+
- [Pattern Matching on Lists](/PatternMatching/README.html)
1212
- [Tasks & Error Handling](/Tasks/README.html)
1313
- [Import Files](/IngestFiles/README.html)
1414
- [Import from Directory](/ImportFromDirectory/README.html)

0 commit comments

Comments
 (0)