format
This commit is contained in:
parent
6e53baaa63
commit
3237af69e5
|
@ -4,14 +4,15 @@ import mipt.utils.Homeworks._
|
|||
|
||||
object task1_pattern_matching {
|
||||
|
||||
type Age = Byte
|
||||
type Age = Byte
|
||||
type Weight = Double
|
||||
type Name = String
|
||||
type Angry = Boolean
|
||||
type Name = String
|
||||
type Angry = Boolean
|
||||
|
||||
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
|
||||
|
@ -28,19 +29,20 @@ object task1_pattern_matching {
|
|||
val prettyFox: DogLike = DogLike(7.5, isForest = false)
|
||||
val fish: Fish = Fish(1, "Nemo")
|
||||
|
||||
private val old = (cat: Cat) => cat.age >= 10
|
||||
private val old = (cat: Cat) => cat.age >= 10
|
||||
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 DogLike(mass, false) if mass < 10 => "Pretty fox"
|
||||
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(_, _) => "Young fat cat"
|
||||
case DogLike(_, false) => "Polar wolf"
|
||||
}
|
||||
animal match {
|
||||
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 DogLike(_, true) => "Forest wolf"
|
||||
case Fish(_, _) => "Nemo"
|
||||
case cat@Cat(_, _) if slim(cat) => "Small kitten"
|
||||
case Cat(_, _) => "Young fat cat"
|
||||
case DogLike(_, false) => "Polar wolf"
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue