Last active
December 11, 2015 05:59
-
-
Save sofoklis/4556379 to your computer and use it in GitHub Desktop.
flatmap option
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 o4 = Option(4) //> o4 : Option[Int] = Some(4) | |
val o5: Option[Int] = None //> o5 : Option[Int] = None | |
val m1 = o4 map ("Num " + _ * 2) //> m1 : Option[String] = Some(Num 8) | |
val m2 = o5 map ("Num " + _ * 2) //> m2 : Option[String] = None | |
val f1 = for(i <- o4) yield "Num " + i * 3//> f1 : Option[String] = Some(Num 12) | |
val f2 = for(i <- o5) yield "Num " + i * 3//> f2 : Option[String] = None | |
val fm1 = o4 flatMap(i => Option("Num " + i * 4)) //> fm1 : Option[String] = Some(Num 16) | |
val fm2 = o5 flatMap(i => Option("Num " + i * 4)) //> fm2 : Option[String] = None | |
o4 foreach (println(_)) //> 4 | |
o5 foreach (println(_)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment