From 98828a841b507948f4e32b43fa5c050a9916bd14 Mon Sep 17 00:00:00 2001 From: comphead Date: Sun, 7 Apr 2019 17:25:27 +0300 Subject: [PATCH 1/2] Update Maps.scala --- src/main/scala/stdlib/Maps.scala | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/scala/stdlib/Maps.scala b/src/main/scala/stdlib/Maps.scala index db3cac53..1b0754de 100644 --- a/src/main/scala/stdlib/Maps.scala +++ b/src/main/scala/stdlib/Maps.scala @@ -160,5 +160,17 @@ object Maps extends FlatSpec with Matchers with org.scalaexercises.definitions.S myMap1.equals(myMap2) should be(res0) } + + /** Map access elements with .get(missingKey) handles nonexistent element gracefully returning Option, instead of throwing `NoSuchElementException` when using `myMap(missingKey)`: + */ + def accessMissingElements(res0: Option[String], res1: Option[String]) { + val myMap = + Map("MI" → "Michigan", "OH" → "Ohio", "WI" → "Wisconsin", "IA" → "Iowa") + intercept[NoSuchElementException] { + myMap("TX") + } + myMap.get("MI") should be(res0) + myMap.get("TX") should be(res1) + } } From 37411ee58bea9fe5774435360cb84751c05f28fd Mon Sep 17 00:00:00 2001 From: comphead Date: Wed, 10 Apr 2019 21:05:33 +0300 Subject: [PATCH 2/2] Update PatternMatching.scala --- src/main/scala/stdlib/PatternMatching.scala | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/scala/stdlib/PatternMatching.scala b/src/main/scala/stdlib/PatternMatching.scala index 5f5621b3..daa31d84 100644 --- a/src/main/scala/stdlib/PatternMatching.scala +++ b/src/main/scala/stdlib/PatternMatching.scala @@ -214,4 +214,23 @@ object PatternMatching extends FlatSpec with Matchers with org.scalaexercises.de r == Nil should be(res0) } + + /** Pattern matching allows complex parsing and assigning an alias(using @) to each part of the parsed type: + */ + def patmatComplexType(res0: String, res1:String, res2: String) { + case class Person(name: String, age: Int) { + def me = "I'm person" + } + + case class Employee(person: Person, job: String) { + def me = "I'm employee" + } + + val res = Employee(Person("John", 23), "Janitor") match { + case e@Employee(p@Person(_, _), job) => + (e.me, p.me, job) + case _ => + } + res should be ((res0,res1,res2)) + } }