Skip to content

Instantly share code, notes, and snippets.

@sofoklis
Created January 20, 2013 11:34
Show Gist options
  • Save sofoklis/4578032 to your computer and use it in GitHub Desktop.
Save sofoklis/4578032 to your computer and use it in GitHub Desktop.
flatmap map
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