In my previous post I introduced Dave and we looked at map, flatten and flatMap. In this new episode we take a look at the Scala Option type:
Here's some code...
case class Person(name: String, partner: Option[Person])
sealed trait Product
case object XBox extends Product
case class WeekendAway(person1: Person, person2: Person) extends Product
type Cash = Int
case class Purchase(product: Product, change: Cash)
def buyXBox(cash: Cash): Purchase = Purchase(XBox, cash - 20000)
def bookWeekendAway(p1: Person, p2: Person, cash: Cash): Purchase = Purchase(WeekendAway(p1, p2), cash - 15000)
val lucy = Person("lucy", None)
val dave = Person("dave", Some(lucy))
//val dave = Person("dave", None)
val wages = 50000
val purchase = dave.partner map (bookWeekendAway(dave, _, wages)) getOrElse buyXBox(wages)
println(purchase)
No comments:
Post a Comment