Skip to content

minor fixes in Maps #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 28, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions src/main/scala/stdlib/Maps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand All @@ -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:
*/
Expand Down
21 changes: 16 additions & 5 deletions src/main/scala/stdlib/PatternMatching.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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"
* }
* }}}
*
Expand All @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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)
}

}
2 changes: 1 addition & 1 deletion src/main/scala/stdlib/Traversables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)))`
*/
Expand Down
18 changes: 9 additions & 9 deletions src/test/scala/stdlib/MapsSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
}
Expand Down
10 changes: 10 additions & 0 deletions src/test/scala/stdlib/PatternMatchingSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,14 @@ class PatternMatchingSpec extends Spec with Checkers {
)
)
}

def `pattern matching lists part five` = {
check(
Test.testSuccess(
PatternMatching.againstListsVPatternMatching _,
true :: HNil
)
)
}

}