Skip to content

Commit ec11b41

Browse files
committed
added another list example
1 parent 630466c commit ec11b41

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

src/main/scala/stdlib/PatternMatching.scala

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,22 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de
189189
*/
190190
def againstListsIVPatternMatching(res0: Int) {
191191
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
193193
case _ 0
194194
}
195195

196196
r should be(res0)
197197
}
198198

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+
199210
}

src/test/scala/stdlib/PatternMatchingSpec.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,14 @@ class PatternMatchingSpec extends Spec with Checkers {
105105
)
106106
)
107107
}
108+
109+
def `pattern matching lists part five` = {
110+
check(
111+
Test.testSuccess(
112+
PatternMatching.againstListsVPatternMatching _,
113+
true :: HNil
114+
)
115+
)
116+
}
117+
108118
}

0 commit comments

Comments
 (0)