forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatch-complete.scala
118 lines (94 loc) · 3.15 KB
/
match-complete.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
object Matcher {
// Chained Match
val chained_match_xs: List[Any] = List(1, 2, 3)
val chained_match_x = chained_match_xs match {
case Nil => "empty"
case _ => "nonempty"
} match {
case "empty" => 0
case "nonempty" => 1
}
println(chained_match_x)
// Vararg Splices
val vararg_arr = Array(0, 1, 2, 3)
val vararg_lst = List(vararg_arr*) // vararg splice argument
// Throws an exception?
val vararg_splice = vararg_lst match
case List(0, 1, xs*) => 1 // binds xs to Seq(2, 3)
case List(1, _*) => 0 // wildcard pattern
case _ => 2
println(vararg_splice)
println(vararg_lst)
// Pattern Definitions
val patter_def_xs: List[Any] = List(1, 2, 3)
val (patter_def_x: Any) :: _ = patter_def_xs : @unchecked
println(patter_def_x)
val patter_def_pair = (1, true)
val (patter_def_a, patter_def_b) = patter_def_pair
println(patter_def_a)
val elems: List[(Int, Int)] = List((1, 2), (3, 4), (5, 6))
for ((x,y) <- elems) do println(x)
def main(args: Array[String]) = {
// println(chained_match_x)
println(vararg_splice)
// println(patter_def_x)
// println(
}
}
// Patter Matching Using Extractors
// Option Extractors
case class Person(name: String, age: Int)
object Person {
def unapply(person: Person): Option[(String, Int)] = Some((person.name, person.age))
}
object OptionMatcher {
val person = Person("Alice", 25)
val result = person match {
case Person(name, age) => s"Name: $name, Age: $age"
case _ => "Not a person"
}
println(result)
}
// Boolean Extractors
object Adult {
def unapply(person: Person): Boolean = person.age >= 18
}
object BooleanMatcher {
val person = Person("Charlie", 17)
val adultResult = person match {
case Adult() => s"${person.name} is an adult"
case _ => s"${person.name} is not an adult"
}
println(adultResult)
}
// Variadic Extractors
// Add cases for exceptions
//
// Adding some warning test cases
// -
object VariadicExtractor {
// Define an unapply method that takes a List and returns an Option of Seq
def unapplySeq[A](list: List[A]): Option[Seq[A]] = Some(list)
}
object PatternMatchExample extends App {
def describeList(list: List[Int]): String = list match {
case VariadicExtractor(1, 2, rest @ _*) =>
s"Starts with 1, 2 followed by: ${rest.mkString(", ")}"
case VariadicExtractor(1, rest @ _*) =>
s"Starts with 1 followed by: ${rest.mkString(", ")}"
case VariadicExtractor(first, second, rest @ _*) =>
s"Starts with $first, $second followed by: ${rest.mkString(", ")}"
case VariadicExtractor(single) =>
s"Only one element: $single"
case VariadicExtractor() =>
"Empty list"
case _ =>
"Unknown pattern"
}
// Test cases
println(describeList(List(1, 2, 3, 4, 5))) // Output: Starts with 1, 2 followed by: 3, 4, 5
println(describeList(List(1, 3, 4, 5))) // Output: Starts with 1 followed by: 3, 4, 5
println(describeList(List(2, 3, 4, 5))) // Output: Starts with 2, 3 followed by: 4, 5
println(describeList(List(1))) // Output: Only one element: 1
println(describeList(List())) // Output: Empty list
}