Skip to content
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

homework_5 - Савичев Александр #154

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
36 changes: 31 additions & 5 deletions homeworks/homework_5/src/main/scala/Exercises.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,42 @@ object Exercises {
case class Dog(override val name: String) extends Animal


case class Shelter[+A <: Animal](pets: List[A]) {
def +[P >: A <: Animal](pet: P): Shelter[P] = {
Shelter[P](pet :: pets)
}

case class Shelter ...
def ++[P >: A <: Animal](another: Shelter[P]): Shelter[P] = {
Shelter(pets ++ another.pets)
}

def getNames: Seq[String] = pets.map(e => e.name)

def feed(food: Food[A]): Seq[String] = {
pets.map(pet => food.feed(pet))
}
}


trait Food ...
trait Food [-A <: Animal] {
def feed(animal: A): String
}

case object Meat extends Food[Animal] ...
case object Meat extends Food[Animal] {
override def toString: String = "meat"

override def feed(animal: Animal): String = s"${animal.name} eats ${toString}"
}

case object Milk extends Food[Cat] ...
case object Milk extends Food[Cat] {
override def toString: String = "milk"

case object Bread extends Food[Dog] ...
override def feed(animal: Cat): String = s"${animal.name} eats ${toString}"
}

case object Bread extends Food[Dog] {
override def toString: String = "bread"

override def feed(animal: Dog): String = s"${animal.name} eats ${toString}"
}
}