Filtering a List (A, Option [B]) and Retrieving a Value from an Option

I have one List[(A, Option[B])]. I would like to filter out all the tuples contained Nonein the second element, and then β€œexpand” Optionby specifying 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)?

+4
source share
1 answer

You can do this with pattern matching and collect:

list.collect { case (a, Some(b)) => (a, b) }
+7
source

All Articles