Skip to content

Example for List as usage (#159) #183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions examples/PatternMatching/PatternMatching.roc
Original file line number Diff line number Diff line change
Expand Up @@ -34,44 +34,56 @@ expect
expect
match = \input ->
when input is
[.., 42] -> EndWith42
[.., 42] -> EndsWith42
_ -> Other

(match [24, 64, 42] == EndWith42)
&& (match [42, 1, 5] != EndWith42)
(match [24, 64, 42] == EndsWith42)
&& (match [42, 1, 5] != EndsWith42)

# Match a list that starts with a Foo tag
# followed by a Bar tag
expect
match = \input ->
when input is
[Foo, Bar, ..] -> FooBar
[Foo, Bar, ..] -> StartsWithFooBar
_ -> Other

(match [Foo, Bar, Bar] == FooBar)
&& (match [Bar, Bar, Foo] != FooBar)
(match [Foo, Bar, Bar] == StartsWithFooBar)
&& (match [Bar, Bar, Foo] != StartsWithFooBar)

# Match a list with these exact elements:
# Foo, Bar, and then (Baz "Hi")
expect
match = \input ->
when input is
[Foo, Bar, Baz "Hi"] -> Bingo
[Foo, Bar, Baz "Hi"] -> FooBarBazStr
_ -> Other

(match [Foo, Bar, Baz "Hi"] == Bingo)
&& (match [Foo, Bar] != Bingo)
&& (match [Foo, Bar, Baz "Hi", Blah] != Bingo)
(match [Foo, Bar, Baz "Hi"] == FooBarBazStr)
&& (match [Foo, Bar] != FooBarBazStr)
&& (match [Foo, Bar, Baz "Hi", Blah] != FooBarBazStr)

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

(match [Foo, Count 1] == FooBar)
&& (match [Foo, Count 0] != FooBar)
&& (match [Baz, Count 1] != FooBar)
(match [Foo, Count 1] == FooCountIf)
&& (match [Foo, Count 0] != FooCountIf)
&& (match [Baz, Count 1] != FooCountIf)

# Use `as` to create a variable equal to the part of the list that matches `..`
expect
match = \input ->
when input is
[head, .. as tail] -> HeadAndTail head tail
_ -> Other

(match [1, 2, 3] == HeadAndTail 1 [2, 3])
&& (match [1, 2] == HeadAndTail 1 [2])
&& (match [1] == HeadAndTail 1 [])
&& (match [] == Other)
28 changes: 25 additions & 3 deletions examples/PatternMatching/README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,28 @@
# Pattern Matching
# Pattern Matching on Lists

Some different ways to do pattern matching in Roc.
All the ways to pattern match on lists:
```roc
when input is
[] -> EmptyList

[_, ..] -> NonEmptyList

["Hi", ..] -> StartsWithHi

[.., 42] -> EndsWith42

[Foo, Bar, ..] -> StartsWithFooBar

[Foo, Bar, Baz "Hi"] -> FooBarBazStr

[Foo, Count num, ..] if num > 0 -> FooCountIf

[head, .. as tail] -> HeadAndTail head tail

_ -> Other
```
Note that this specific snippet would not typecheck because it uses lists of different types.
This is just meant to be a compact overview. See the code section below for valid Roc.

## Code
```roc
Expand All @@ -14,5 +36,5 @@ Run this from the directory that has `PatternMatching.roc` in it:
```
$ roc test PatternMatching.roc

0 failed and 7 passed in 629 ms.
0 failed and 8 passed in 88 ms.
```
2 changes: 1 addition & 1 deletion examples/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ You can find the source code for all of these at [github.com/roc-lang/examples](
- [FizzBuzz](/FizzBuzz/README.html)
- [Basic Dict Usage](/BasicDict/README.html)
- [Tuples](/Tuples/README.html)
- [Pattern Matching](/PatternMatching/README.html)
- [Pattern Matching on Lists](/PatternMatching/README.html)
- [Tasks & Error Handling](/Tasks/README.html)
- [Import Files](/IngestFiles/README.html)
- [Import from Directory](/ImportFromDirectory/README.html)
Expand Down