Created
March 16, 2016 01:46
-
-
Save ludflu/90e5f5bd84dd154d8396 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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