From 183ebbf7e16ce4f024faf168722b4ff0faafa191 Mon Sep 17 00:00:00 2001 From: "Jiahui (Jerry) Tan" <66892505+Friendseeker@users.noreply.github.com> Date: Sat, 23 Nov 2024 18:47:19 -0800 Subject: [PATCH 1/4] Add documentation for Pattern Matching on String --- _tour/pattern-matching.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/_tour/pattern-matching.md b/_tour/pattern-matching.md index a89c16775f..b33ea88b54 100644 --- a/_tour/pattern-matching.md +++ b/_tour/pattern-matching.md @@ -138,6 +138,36 @@ println(showNotification(someVoiceRecording)) // prints You received a Voice Re The function `showNotification` takes as a parameter the abstract type `Notification` and matches on the type of `Notification` (i.e. it figures out whether it's an `Email`, `SMS`, or `VoiceRecording`). In the `case Email(sender, title, _)` the fields `sender` and `title` are used in the return value but the `body` field is ignored with `_`. +## Matching on String + +The `s`-interpolator allows embedding variables in strings and is also useful for pattern matching. + +{% tabs s-interpolator-pattern-matching class=tabs-scala-version %} +{% tab 'Scala 2' for=s-interpolator-pattern-matching %} +```scala +val input: String = "Alice is 25 years old" + +input match { + case s"$name is $age years old" => s"$name's age is $age" + case _ => "No match" +} +// Result: "Alice's age is 25" +``` +{% endtab %} +{% tab 'Scala 3' for=s-interpolator-pattern-matching %} +```scala +val input: String = "Alice is 25 years old" + +input match + case s"$name is $age years old" => s"$name's age is $age" + case _ => "No match" +// Result: "Alice's age is 25" +``` +{% endtab %} +{% endtabs %} + +In this example, name and age extract parts of the string based on the pattern. This is helpful for parsing structured text. + ## Pattern guards Pattern guards are boolean expressions which are used to make cases more specific. Just add `if ` after the pattern. From 484ff424d8ca52626da3ff11a33da5ce4b6efd58 Mon Sep 17 00:00:00 2001 From: "Jiahui (Jerry) Tan" <66892505+Friendseeker@users.noreply.github.com> Date: Sat, 23 Nov 2024 18:55:51 -0800 Subject: [PATCH 2/4] Change String -> string as other h2 headings use lower case --- _tour/pattern-matching.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/_tour/pattern-matching.md b/_tour/pattern-matching.md index b33ea88b54..51108b99e2 100644 --- a/_tour/pattern-matching.md +++ b/_tour/pattern-matching.md @@ -138,7 +138,7 @@ println(showNotification(someVoiceRecording)) // prints You received a Voice Re The function `showNotification` takes as a parameter the abstract type `Notification` and matches on the type of `Notification` (i.e. it figures out whether it's an `Email`, `SMS`, or `VoiceRecording`). In the `case Email(sender, title, _)` the fields `sender` and `title` are used in the return value but the `body` field is ignored with `_`. -## Matching on String +## Matching on string The `s`-interpolator allows embedding variables in strings and is also useful for pattern matching. From e4bbc9b776db084e780f6e035434919511dbf88a Mon Sep 17 00:00:00 2001 From: "Jiahui (Jerry) Tan" <66892505+Friendseeker@users.noreply.github.com> Date: Sat, 23 Nov 2024 20:18:02 -0800 Subject: [PATCH 3/4] Add note on extractor object --- _tour/pattern-matching.md | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/_tour/pattern-matching.md b/_tour/pattern-matching.md index 51108b99e2..d104ed356f 100644 --- a/_tour/pattern-matching.md +++ b/_tour/pattern-matching.md @@ -168,6 +168,39 @@ input match In this example, name and age extract parts of the string based on the pattern. This is helpful for parsing structured text. +We can also use extractor objects for string pattern matching. + +{% tabs s-interpolator-pattern-matching-2 class=tabs-scala-version %} +{% tab 'Scala 2' for=s-interpolator-pattern-matching-2 %} +```scala +object Int { + def unapply(s: String): Option[Int] = s.toIntOption +} + +val input: String = "Alice is 25 years old" + +val (name, age) = input match { + case s"$name is ${Int(age)} years old" => (name, age) +} +// name: String = Alice +// age: Int = 25 +``` +{% endtab %} +{% tab 'Scala 3' for=s-interpolator-pattern-matching-2 %} +```scala +object Int: + def unapply(s: String): Option[Int] = s.toIntOption + +val input: String = "Alice is 25 years old" + +val (name, age) = input match + case s"$name is ${Int(age)} years old" => (name, age) +// name: String = Alice +// age: Int = 25 +``` +{% endtab %} +{% endtabs %} + ## Pattern guards Pattern guards are boolean expressions which are used to make cases more specific. Just add `if ` after the pattern. From 6b3d1f278cda68e799d5312f0328efacdd89caa3 Mon Sep 17 00:00:00 2001 From: "Jiahui (Jerry) Tan" <66892505+Friendseeker@users.noreply.github.com> Date: Mon, 25 Nov 2024 00:55:24 -0800 Subject: [PATCH 4/4] Adopt feedback from adpi2 --- _tour/pattern-matching.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/_tour/pattern-matching.md b/_tour/pattern-matching.md index d104ed356f..e2a3929010 100644 --- a/_tour/pattern-matching.md +++ b/_tour/pattern-matching.md @@ -173,14 +173,14 @@ We can also use extractor objects for string pattern matching. {% tabs s-interpolator-pattern-matching-2 class=tabs-scala-version %} {% tab 'Scala 2' for=s-interpolator-pattern-matching-2 %} ```scala -object Int { +object Age { def unapply(s: String): Option[Int] = s.toIntOption } val input: String = "Alice is 25 years old" val (name, age) = input match { - case s"$name is ${Int(age)} years old" => (name, age) + case s"$name is ${Age(age)} years old" => (name, age) } // name: String = Alice // age: Int = 25 @@ -188,13 +188,13 @@ val (name, age) = input match { {% endtab %} {% tab 'Scala 3' for=s-interpolator-pattern-matching-2 %} ```scala -object Int: +object Age: def unapply(s: String): Option[Int] = s.toIntOption val input: String = "Alice is 25 years old" val (name, age) = input match - case s"$name is ${Int(age)} years old" => (name, age) + case s"$name is ${Age(age)} years old" => (name, age) // name: String = Alice // age: Int = 25 ```