scala turning an Iterator[Option[T]] into an Iterator[T]
-
21-08-2019 - |
Question
I have an Iterator[Option[T]]
and I want to get an Iterator[T]
for those Option
s where T
isDefined
. There must be a better way than this:
it filter { _ isDefined} map { _ get }
I would have thought that it was possible in one construct... Anybody any ideas?
Solution
In the case where it
is an Iterable
val it:Iterable[Option[T]] = ...
it.flatMap( x => x ) //returns an Iterable[T]
In the case where it
is an Iterator
val it:Iterator[Option[T]] = ...
it.flatMap( x => x elements ) //returns an Iterator[T]
it.flatMap( _ elements) //equivalent
OTHER TIPS
In newer versions this is now possible:
val it: Iterator[Option[T]] = ...
val flatIt = it.flatten
This works for me (Scala 2.8):
it.collect {case Some(s) => s}
To me, this is a classic use case for the monadic UI.
for {
opt <- iterable
t <- opt
} yield t
It's just sugar for the flatMap
solution described above, and it produces identical bytecode. However, syntax matters, and I think one of the best times to use Scala's monadic for
syntax is when you're working with Option
, especially in conjunction with collections.
I think this formulation is considerably more readable, especially for those not very familiar with functional programming. I often try both the monadic and functional expressions of a loop and see which seems more straightforward. I think flatMap is hard name for most people to grok (and actually, calling it >>=
makes more intuitive sense to me).