I have a Map[A, Option[B]]
, what is the optimal way to make a flatten to get a Map[A, B]
?
I know for a list we can use flatten, but this structure is different
Well, they are not the same, so you will need a way to define what happens if a value is None. I assume you want to ignore those keys, if so, you can collect with a partial function:
map.collect {
case (k, Some(v)) => k -> v
}
or use a for-comprehension
for ((k, Some(v)) <- map) yield k -> v
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With