Say I have a val s: Option[Option[String]]
. It can thus have the following values:
Some(Some(\"foo\"))
Some(None)
None
You might use flatMap like the following:
val options = List(Some(Some(1)), Some(None), None)
options map (_ flatMap (a => a))
This will map the List[Option[Option[Int]]]
to a List[Option[Int]]
.
If you just have an Option you can use it as following:
val option = Some(Some(2))
val unzippedOption = option flatMap (b => b)
This will flatten your Option[Option[Int]]
to Option[Int]
.