Skip to content

Instantly share code, notes, and snippets.

@taitruong
Last active August 29, 2015 13:57
Show Gist options
  • Save taitruong/9786648 to your computer and use it in GitHub Desktop.
Save taitruong/9786648 to your computer and use it in GitHub Desktop.
Option, pattern matching, map, toList
object ScalaHackSession {
val some:Option[Int] = Some(1) //> some : Option[Int] = Some(1)
val none:Option[Int] = None //> none : Option[Int] = None
def map(option: Option[Int]) = option match {
case None => None
case Some(x) => Some(x*10)
} //> map: (option: Option[Int])Option[Int]
map(some) //> res0: Option[Int] = Some(10)
map(none) //> res1: Option[Int] = None
def toList(option: Option[Int]) = option match {
case None => Nil
case Some(x) => List(x)
} //> toList: (option: Option[Int])List[Int]
toList(some) //> res2: List[Int] = List(1)
toList(none) //> res3: List[Int] = List()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment