Last active
August 29, 2015 13:57
-
-
Save taitruong/9786648 to your computer and use it in GitHub Desktop.
Option, pattern matching, map, toList
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
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