diff --git a/src/main/scala/stdlib/Maps.scala b/src/main/scala/stdlib/Maps.scala index 0a2e7967..8e4fd055 100644 --- a/src/main/scala/stdlib/Maps.scala +++ b/src/main/scala/stdlib/Maps.scala @@ -24,7 +24,7 @@ object Maps extends FlatSpec with Matchers with org.scalaexercises.definitions.S myMap.size should be(res0) } - /** Maps contain distinct pairings: + /** Maps do not contain multiple identical pairs: */ def distinctPairingsMaps(res0: Int) { val myMap = Map("MI" → "Michigan", "OH" → "Ohio", "WI" → "Wisconsin", "MI" → "Michigan") @@ -50,6 +50,15 @@ object Maps extends FlatSpec with Matchers with org.scalaexercises.definitions.S val lastElement = mapValues.last lastElement should be(res2) //Failed presumption: The order in maps is not guaranteed + + } + + /** Maps may be accessed: + */ + def mayBeAccessedMaps(res0: String, res1: String) { + val myMap = Map("MI" → "Michigan", "OH" → "Ohio", "WI" → "Wisconsin", "IA" → "Iowa") + myMap("MI") should be(res0) + myMap("IA") should be(res1) } /** Maps insertion with duplicate key updates previous entry with subsequent value: @@ -70,14 +79,6 @@ object Maps extends FlatSpec with Matchers with org.scalaexercises.definitions.S myMap(49931) should be(res1) } - /** Maps may be accessed: - */ - def mayBeAccessedMaps(res0: String, res1: String) { - val myMap = Map("MI" → "Michigan", "OH" → "Ohio", "WI" → "Wisconsin", "IA" → "Iowa") - myMap("MI") should be(res0) - myMap("IA") should be(res1) - } - /** If a nonexistent map key is requested using `myMap(missingKey)`, a `NoSuchElementException` will be thrown. * Default values may be provided using either `getOrElse` or `withDefaultValue` for the entire map: */ diff --git a/src/main/scala/stdlib/PatternMatching.scala b/src/main/scala/stdlib/PatternMatching.scala index 7c94c0b3..b6b13492 100644 --- a/src/main/scala/stdlib/PatternMatching.scala +++ b/src/main/scala/stdlib/PatternMatching.scala @@ -15,9 +15,9 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de * def matchTest(x: Int): String = x match { * case 1 => "one" * case 2 => "two" - * case _ => "many" + * case _ => "many" // case _ will trigger if all other cases fail. * } - * println(matchTest(3)) + * println(matchTest(3)) // prints "many" * } * }}} * @@ -38,7 +38,7 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de println("BLUE"); 2 case "green" ⇒ println("GREEN"); 3 - case _ ⇒ println(stuff); 0 //case _ will trigger if all other cases fail. + case _ ⇒ println(stuff); 0 // case _ will trigger if all other cases fail. } myStuff should be(res0) @@ -178,7 +178,7 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de */ def againstListsIIIPatternMatching(res0: Int) { val secondElement = List(1) match { - case x :: y :: xs ⇒ y + case x :: y :: xs ⇒ y // only matches a list with two or more items case _ ⇒ 0 } @@ -189,11 +189,22 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de */ def againstListsIVPatternMatching(res0: Int) { val r = List(1, 2, 3) match { - case x :: y :: Nil ⇒ y + case x :: y :: Nil ⇒ y // only matches a list with exactly two items case _ ⇒ 0 } r should be(res0) } + /** If a pattern is exactly one element longer than a `List`, it extracts the final `Nil`: + */ + def againstListsVPatternMatching(res0: Boolean) { + val r = List(1, 2, 3) match { + case x :: y :: z :: tail ⇒ tail + case _ ⇒ 0 + } + + r == Nil should be(res0) + } + } diff --git a/src/main/scala/stdlib/Traversables.scala b/src/main/scala/stdlib/Traversables.scala index b044eafc..cb7cf9b5 100644 --- a/src/main/scala/stdlib/Traversables.scala +++ b/src/main/scala/stdlib/Traversables.scala @@ -442,7 +442,7 @@ object Traversables extends FlatSpec with Matchers with org.scalaexercises.defin (((((0 - 5) - 4) - 3) - 2) - 1) should be(res4) } - /** `:\` or foldRight` will combine an operation starting with a seed and combining from the right. Fold right is defined as (list :\ seed), where seed is the initial value. Once the fold is established, you provide a function that takes two elements. The first is the next element of the list, and the second element is the running total of the operation. + /** `:\` or `foldRight` will combine an operation starting with a seed and combining from the right. Fold right is defined as (list :\ seed), where seed is the initial value. Once the fold is established, you provide a function that takes two elements. The first is the next element of the list, and the second element is the running total of the operation. * * Given a `Traversable (x1, x2, x3, x4)`, an initial value of `init`, an operation `op`, `foldRight` is defined as: `x1 op (x2 op (x3 op (x4 op init)))` */ diff --git a/src/test/scala/stdlib/MapsSpec.scala b/src/test/scala/stdlib/MapsSpec.scala index 12655872..9e223322 100644 --- a/src/test/scala/stdlib/MapsSpec.scala +++ b/src/test/scala/stdlib/MapsSpec.scala @@ -43,29 +43,29 @@ class MapsSpec extends Spec with Checkers { ) } - def `duplicate keys` = { + def `map key access` = { check( Test.testSuccess( - Maps.duplicatedKeyInsertionMaps _, - 3 :: "Meechigan" :: HNil + Maps.mayBeAccessedMaps _, + "Michigan" :: "Iowa" :: HNil ) ) } - def `mixed key types` = { + def `duplicate keys` = { check( Test.testSuccess( - Maps.mixedTypeKeysMaps _, - "MI" :: "MI" :: HNil + Maps.duplicatedKeyInsertionMaps _, + 3 :: "Meechigan" :: HNil ) ) } - def `map key access` = { + def `mixed key types` = { check( Test.testSuccess( - Maps.mayBeAccessedMaps _, - "Michigan" :: "Iowa" :: HNil + Maps.mixedTypeKeysMaps _, + "MI" :: "MI" :: HNil ) ) } diff --git a/src/test/scala/stdlib/PatternMatchingSpec.scala b/src/test/scala/stdlib/PatternMatchingSpec.scala index 45f80431..3d880625 100644 --- a/src/test/scala/stdlib/PatternMatchingSpec.scala +++ b/src/test/scala/stdlib/PatternMatchingSpec.scala @@ -105,4 +105,14 @@ class PatternMatchingSpec extends Spec with Checkers { ) ) } + + def `pattern matching lists part five` = { + check( + Test.testSuccess( + PatternMatching.againstListsVPatternMatching _, + true :: HNil + ) + ) + } + }