Skip to content

Instantly share code, notes, and snippets.

@ludflu
Created March 16, 2016 01:46
Show Gist options
  • Save ludflu/90e5f5bd84dd154d8396 to your computer and use it in GitHub Desktop.
Save ludflu/90e5f5bd84dd154d8396 to your computer and use it in GitHub Desktop.
def parseInt1( s : String) : Option[Int] = {
try {
Some(s.toInt)
} catch {
case _ : Throwable => None
}
}
def parseInt2(s: String): Option[Int] = {
Try(s.toInt) match {
case Success(i) => Some(i)
case Failure(_) => None
}
}
def mkOptions[A,B]( trans: (A => B)) : (A => Option[B]) = {
a => scala.util.Try(trans(a)) match {
case scala.util.Success(i) => Some(i)
case scala.util.Failure(_) => None
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment