This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Set[A] { | |
def get(index: Long): A | |
def add(value: A): Set[A] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Perishable { | |
def expired: Boolean | |
} | |
case class Baguette() extends Perishable { | |
def expired: Boolean = ??? | |
} | |
case class Croissant() extends Perishable { | |
def expired: Boolean = ??? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait DailyOrderService { | |
def needsReplacing(perishables: List[Perishable]): List[Perishable] = { | |
perishables.filter(_.expired) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait PerishableLike[A] { | |
def expired: Boolean | |
} | |
case class PerishableLikeBaguette(b: Baguette) extends PerishableLike[Baguette] { | |
def expired: Boolean = ??? | |
} | |
case class PerishableLikeCroissant(c: Croissant) extends PerishableLike[Croissant] { | |
def expired: Boolean = ??? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def needsReplacing[T](perishables: List[PerishableLike[T]]): List[PerishableLike[T]] = { | |
perishables.filter(_.expired) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
trait Perishable[T] { | |
def expired(item: T): Boolean | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
case class Baguette() | |
case class Croissant() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object Perishables { | |
implicit val baguettePerishable: Perishable[Baguette] = new Perishable[Baguette] { | |
def expired(b: Baguette): Boolean = false // hard coded, in reality we would examine `b` to figure out if it expired | |
} | |
implicit val croissantPerishable: Perishable[Croissant] = new Perishable[Croissant] { | |
def expired(c: Croissant): Boolean = false | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scala> import Perishables._ | |
import Perishables._ | |
scala> DailyOrderService.needsReplacing(List(Baguette(), Baguette(), Baguette())) | |
res0: List[Baguette] = List() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object OutsourcedPerishables { | |
implicit val iceCreamPerishable: Perishable[IceCream] = new Perishable[IceCream] { | |
def expired(i: IceCream): Boolean = true // in reality you would actually calculate the expiration for ice cream | |
} | |
} |