Skip to content

Instantly share code, notes, and snippets.

View madeleine-chercover-hs's full-sized avatar

madeleine-chercover-hs

View GitHub Profile
def match(x: Int): String = x match {
case 1 => "one"
case 2 => "two"
case _ => "whatever"
}
sealed abstract class Vegetable
case class Pea(colour: String, isCooked: Boolean) extends Vegetable
case class Eggplant(colour: String, weight: Int) extends Vegetable
case class ChocolateCake(layers: Int, isVegetable: Boolean = true) extends Vegetable
def eatVegetable(vegetable: Vegetable): String =
vegetable match {
case Pea(colour, isCooked) =>
s"Oh great, a ${if (isCooked) "uncooked" else "cooked" } $colour pea."
case Eggplant(colour, _) =>
s"No matter how I cook this $colour eggplant, it'll still be the blandest vegetable around."
case ChocolateCake(layers, _) =>
s"Yum, a $layers-layer chocolate cake – the best vegetable of all."
}
def eatVegetable(vegetable: Vegetable): String =
vegetable match {
case ChocolateCake(layers, _) if layers > 3 =>
"Now that's what I call a cake - I mean vegetable."
case _ =>
"No, thank you."
}