I have a Sequence of Cell | None, so I use Option[Cell]
I am in a scenario where I need to filter the sequence for all existing Cells (so where Some(cell) is matched, instead of None). All ways I have found look computationally expensive as they involve flattening and them mapping. I assume the syntax is very simple and I’m just not getting it right.
val row: Seq[Option[Cell]] = ???
val cells: Seq[Cell] = row.filter(_.isDefined)
Yes, in this case the result type is determined by .filter, which does not change the type. Here’s the type signature of filter it still returns a Seq[A] which in your case is Seq[Option[Cell]] because A = Option[Cell].
.flatten as suggested above is the right approach here, because it does change the type as you want. Here is the type signature of flatten, here the IterableOnce[B] = Option[Cell], so B = Cell and as you see it returns Seq[B] which is Seq[Cell].
In that case you can use .flatMap which does those things together.
More generally, you can use .view to create “lazy collections”, then chain together as many transformations as you want, then at the end use .to (or .to[sometype] or .toSeq, etc.) for the final result you want. This avoids the computational expense.