Created
January 20, 2013 11:34
-
-
Save sofoklis/4578032 to your computer and use it in GitHub Desktop.
flatmap map
This file contains hidden or 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
val n : Option[Int] = None //> n : Option[Int] = None | |
val ol = List(Option(1), n , Option(3), Option(5), n) //> ol : List[Option[Int]] = List(Some(1), None, Some(3), Some(5), None) | |
ol map ( i => i map (j => j * 2)) //> res3: List[Option[Int]] = List(Some(2), None, Some(6), Some(10), None) | |
// FlatMap with map and same using For comprehension | |
ol flatMap ( i => i map (j => Option(j * 2))) //> res5: List[Option[Int]] = List(Some(2), Some(6), Some(10)) | |
for(o <- ol; i <- o) yield Option(i*2) //> res6: List[Option[Int]] = List(Some(2), Some(6), Some(10)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment