Skip to content

Instantly share code, notes, and snippets.

@heathermiller
Created June 27, 2012 15:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heathermiller/3004675 to your computer and use it in GitHub Desktop.
Save heathermiller/3004675 to your computer and use it in GitHub Desktop.
Illustrative use case for transform on scala.util.Try
def result(i: Int, d: Double, b: Boolean) = {
if (b) d else i
}
def fA(s: String) = 7
def fB(s: String, i: Int) = 1.0
def fC(s: String, i: Int, d: Double) = true
import scala.util.{Try, Success, Failure}
def test(s: String) = {
Try(fA(s)).transform(
ea => Success(result(0, 0.0, false)), a => Try(fB(s, a)).transform(
eb => Success(result(a, 0.0, false)), b => Try(fC(s, a, b)).transform(
ec => Success(result(a, b, false)), c => Try(result(a, b, c))
)
)
).get
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment