If I have something like a List[Option[A]]
and I want to convert this into a List[A]
, the standard way is to use flatMap
:
scala> val l = List(Some("Hello"), None, Some("World"))
l: List[Option[java.lang.String]] = List(Some(Hello), None, Some(World))
scala> l.flatMap( o => o)
res0: List[java.lang.String] = List(Hello, World)
Now o => o
is just an identity function. I would have thought there'd be some way to do:
l.flatMap(Identity) //return a List[String]
However, I can't get this to work as you can't generify an object
. I tried a few things to no avail; has anyone got something like this to work?