This commit is contained in:
Timofey Khoruzhii 2023-02-25 18:45:09 +03:00
parent 6e53baaa63
commit 3237af69e5

View file

@ -12,6 +12,7 @@ object task1_pattern_matching {
sealed trait Animal
task"Добавьте классы extends Animal такие, что бы могли быть определены закоментированные переменные ниже. Используйте тип Age для возраста, Weight для массы тела и Name для клички животного."
case class Cat(age: Age, mass: Weight) extends Animal
case class DogLike(mass: Weight, isForest: Boolean) extends Animal
@ -32,14 +33,15 @@ object task1_pattern_matching {
private val slim = (cat: Cat) => cat.mass <= 7
task"Заполните пробелы в pattern matching. Используйте для ограждения функции old и slim в случае кошек."
def matching(animal: Animal): String =
animal match {
case cat @ Cat(_, _) if old(cat) && !slim(cat) => "Old fat cat"
case cat@Cat(_, _) if old(cat) && !slim(cat) => "Old fat cat"
case DogLike(mass, false) if mass < 10 => "Pretty fox"
case cat @ Cat(_, _) if old(cat) && slim(cat) => "Old slim cat"
case cat@Cat(_, _) if old(cat) && slim(cat) => "Old slim cat"
case DogLike(_, true) => "Forest wolf"
case Fish(_, _) => "Nemo"
case cat @ Cat(_, _) if slim(cat) => "Small kitten"
case cat@Cat(_, _) if slim(cat) => "Small kitten"
case Cat(_, _) => "Young fat cat"
case DogLike(_, false) => "Polar wolf"
}