Using Scala 2.7.7:
If I have a list of Options, I can flatten them using a for-comprehension:
val listOfOptions = List(None, Some("hi"), None)
listOfOptions: List[Option[java.lang.String]] = List(None, Some(hi), None)
scala> for (opt <- listOfOptions; string <- opt) yield string
res0: List[java.lang.String] = List(hi)
I don't like this style, and would rather use a HOF. This attempt is too verbose to be acceptable:
scala> listOfOptions.flatMap(opt => if (opt.isDefined) Some(opt.get) else None)
res1: List[java.lang.String] = List(hi)
Intuitively I would have expected the following to work, but it doesn't:
scala> List.flatten(listOfOptions)
<console>:6: error: type mismatch;
found : List[Option[java.lang.String]]
required: List[List[?]]
List.flatten(listOfOptions)
Even the following seems like it should work, but doesn't:
scala> listOfOptions.flatMap(_: Option[String])
<console>:6: error: type mismatch;
found : Option[String]
required: (Option[java.lang.String]) => Iterable[?]
listOfOptions.flatMap(_: Option[String])
^
The best I can come up with is:
scala> listOfOptions.flatMap(_.toList)
res2: List[java.lang.String] = List(hi)
... but I would much rather not have to convert the option to a list. That seems clunky.
Any advice?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…