From 0958a5b49e69e6d71f5b8cb9084b41700aea2d05 Mon Sep 17 00:00:00 2001 From: hellrich Date: Sat, 11 Mar 2017 15:22:58 +0100 Subject: [PATCH 1/7] minor fixes in Maps --- src/main/scala/stdlib/Maps.scala | 19 ++++++++++--------- src/test/scala/stdlib/MapsSpec.scala | 18 +++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) diff --git a/src/main/scala/stdlib/Maps.scala b/src/main/scala/stdlib/Maps.scala index e70dd118..103631a1 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 map key is requested using myMap(missingKey) which does not exist a NoSuchElementException will be thrown. * Default values may be provided using either getOrElse or withDefaultValue for the entire map */ 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 ) ) } From 92fa54136a777cad7f5ce64d918a581b8401c80f Mon Sep 17 00:00:00 2001 From: hellrich Date: Sat, 11 Mar 2017 16:37:18 +0100 Subject: [PATCH 2/7] added printed output as comment --- src/main/scala/stdlib/PatternMatching.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/stdlib/PatternMatching.scala b/src/main/scala/stdlib/PatternMatching.scala index 5dbdeebb..d40d89fc 100644 --- a/src/main/scala/stdlib/PatternMatching.scala +++ b/src/main/scala/stdlib/PatternMatching.scala @@ -17,7 +17,7 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de * case 2 => "two" * case _ => "many" * } - * println(matchTest(3)) + * println(matchTest(3)) // prints "many" * } * }}} * From 86f821be51f2b6d322636fcf5ae4ff164dcac3ff Mon Sep 17 00:00:00 2001 From: hellrich Date: Sat, 11 Mar 2017 16:39:43 +0100 Subject: [PATCH 3/7] additional comment, comment formatting --- src/main/scala/stdlib/PatternMatching.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/scala/stdlib/PatternMatching.scala b/src/main/scala/stdlib/PatternMatching.scala index d40d89fc..6509f9a1 100644 --- a/src/main/scala/stdlib/PatternMatching.scala +++ b/src/main/scala/stdlib/PatternMatching.scala @@ -15,7 +15,7 @@ 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)) // 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) From 630466cf994fcd082101fc619fbdaedc8ba0769b Mon Sep 17 00:00:00 2001 From: hellrich Date: Sat, 11 Mar 2017 17:31:21 +0100 Subject: [PATCH 4/7] additional comment --- src/main/scala/stdlib/PatternMatching.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/scala/stdlib/PatternMatching.scala b/src/main/scala/stdlib/PatternMatching.scala index 6509f9a1..08cf2a3a 100644 --- a/src/main/scala/stdlib/PatternMatching.scala +++ b/src/main/scala/stdlib/PatternMatching.scala @@ -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 } From ec11b4125a6fe98666be488aaeb2026e20bdaf55 Mon Sep 17 00:00:00 2001 From: hellrich Date: Sat, 11 Mar 2017 17:56:54 +0100 Subject: [PATCH 5/7] added another list example --- src/main/scala/stdlib/PatternMatching.scala | 13 ++++++++++++- src/test/scala/stdlib/PatternMatchingSpec.scala | 10 ++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/main/scala/stdlib/PatternMatching.scala b/src/main/scala/stdlib/PatternMatching.scala index 08cf2a3a..5b7ca27b 100644 --- a/src/main/scala/stdlib/PatternMatching.scala +++ b/src/main/scala/stdlib/PatternMatching.scala @@ -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/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 + ) + ) + } + } From ca5fc6d57d382c374c195fce03c8a327a3496eb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jeronimo=20L=C3=B3pez?= Date: Tue, 14 Mar 2017 23:19:56 +0100 Subject: [PATCH 6/7] Fix foldRight code quote In foldRightFunctionTraversables explanation, foldRight is not quoted correctly and the rest of phrase is rendered as code. --- src/main/scala/stdlib/Traversables.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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)))` */ From 0cf93ce5507e230942deaf319773f57ebca7f630 Mon Sep 17 00:00:00 2001 From: hellrich Date: Sat, 11 Mar 2017 15:22:58 +0100 Subject: [PATCH 7/7] minor fixes in Maps --- src/main/scala/stdlib/Maps.scala | 19 ++++++++++--------- src/test/scala/stdlib/MapsSpec.scala | 18 +++++++++--------- 2 files changed, 19 insertions(+), 18 deletions(-) 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/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 ) ) }