@@ -15,9 +15,9 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de
15
15
* def matchTest(x: Int): String = x match {
16
16
* case 1 => "one"
17
17
* case 2 => "two"
18
- * case _ => "many"
18
+ * case _ => "many" // case _ will trigger if all other cases fail.
19
19
* }
20
- * println(matchTest(3))
20
+ * println(matchTest(3)) // prints "many"
21
21
* }
22
22
* }}}
23
23
*
@@ -38,7 +38,7 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de
38
38
println(" BLUE" ); 2
39
39
case " green" ⇒
40
40
println(" GREEN" ); 3
41
- case _ ⇒ println(stuff); 0 // case _ will trigger if all other cases fail.
41
+ case _ ⇒ println(stuff); 0 // case _ will trigger if all other cases fail.
42
42
}
43
43
44
44
myStuff should be(res0)
@@ -178,7 +178,7 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de
178
178
*/
179
179
def againstListsIIIPatternMatching (res0 : Int ) {
180
180
val secondElement = List (1 ) match {
181
- case x :: y :: xs ⇒ y
181
+ case x :: y :: xs ⇒ y // only matches a list with two or more items
182
182
case _ ⇒ 0
183
183
}
184
184
@@ -189,11 +189,22 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de
189
189
*/
190
190
def againstListsIVPatternMatching (res0 : Int ) {
191
191
val r = List (1 , 2 , 3 ) match {
192
- case x :: y :: Nil ⇒ y
192
+ case x :: y :: Nil ⇒ y // only matches a list with exactly two items
193
193
case _ ⇒ 0
194
194
}
195
195
196
196
r should be(res0)
197
197
}
198
198
199
+ /** If a pattern is exactly one element longer than a `List`, it extracts the final `Nil`:
200
+ */
201
+ def againstListsVPatternMatching (res0 : Boolean ) {
202
+ val r = List (1 , 2 , 3 ) match {
203
+ case x :: y :: z :: tail ⇒ tail
204
+ case _ ⇒ 0
205
+ }
206
+
207
+ r == Nil should be(res0)
208
+ }
209
+
199
210
}
0 commit comments