Reputation: 2453
I have defined:
final case class EventOpt(start: Option[Long], end: Option[Long])
final case class Event(start: Long, end: Long)
The only interesting objects for me are those with both fields set and I want to filter the rest
Having List[EventOpt]
I'd like to convert it to List[Option[Event]]
using Some(Event)
when both start
and end
are set and None
when either of start
and end
them is None
?
Eg.
List(EventOpt(Some(1), None), EventOpt(None, Some(2)), EventOpt(Some(3), Some(4)))
=>
List(None, None, Some(Event(4, 3)))
By doing so, it will allow be to do a simple flatMap
over the last list and omit the None
s
Upvotes: 2
Views: 38
Reputation: 44908
You don't need a separate flatMap
step, simply collect
only what you need right away:
eventOpts.collect { case EventOpt(Some(x), Some(y)) => Event(x, y) }
Upvotes: 6