Skip to content

Instantly share code, notes, and snippets.

@apivovarov
Created January 21, 2016 21:17
Show Gist options
  • Save apivovarov/69dffbb06a54320a58e6 to your computer and use it in GitHub Desktop.
Save apivovarov/69dffbb06a54320a58e6 to your computer and use it in GitHub Desktop.
import scala.util.{Failure, Success, Try}
val f1: (Int) => Try[String] = x => Try(s"f1($x)")
val f2: (String) => Try[String] = y => Try(s"f2($y)")
val f3: (String) => Try[String] = z => Try(s"f3($z)")
val x = 10
val res: Try[String] = f1(x).flatMap(y => f2(y).flatMap(z => f3(z)))
println(res)
// Success(f3(f2(f1(10))))
val res2: Try[String] = f1(x) match {
case Success(y) => f2(y) match {
case Success(z) => f3(z)
case e2@Failure(_) => e2
}
case e1@Failure(_) => e1
}
println(res2)
// Success(f3(f2(f1(10))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment