A relaxed chat room about all things Scala. Beginner questions welcome. http://scala-lang.org/conduct/ applies
sealed trait Expression{
def eval: Sum[String, Double] = this match {
case Addition(left, right) => left.eval flatMap { x => right.eval flatMap(y => Success(x + y))}
case Subtraction(left, right) => left.eval flatMap { x => right.eval flatMap(y => Success(x - y))}
}
flatMap
on? Success is one of the members and left and right both have that type
map
, so yield
yield f(x, y)
for/yield
and Option
private def calculateShippingPriceRedo(cookie: String, country: Option[String], state: Option[String], shippingMethodId: Option[Int] = None): Future[Option[Double]] = {
val countryWithDefault = country.getOrElse("US")
shippingOptionsFull(cookie, country=countryWithDefault, state).map{ opt => opt.map{ rows =>
shippingMethodId.flatMap{ id =>
rows.find(_.shippingSpeedType === id ).orElse(rows.headOption).map(_.charge)
}
}.toOption.flatten
}
}
OptionT
to add Future[Option[Double]]
, right?
Nested
is a better choice.
@ val a = Future(Option(42))
a: Future[Option[Int]] = Future(Success(Some(42)))
@ (Nested(a), Nested(a)).mapN(_ + _).value
res7: Future[Option[Int]] = Future(Success(Some(84)))