I have a List[(A, Option[B])]
. I would like to filter out all of the tuples containing a None
in the second element and then "unwrap" the Option
, giving a List[A, B]
.
I am currently using this:
list.filter(_._2.isDefined).map(tup => (tup._1, tup._2.get))
Is there a better way (more concise)?
You can do it with pattern matching and collect
:
list.collect { case (a, Some(b)) => (a, b) }
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