Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Scala have an API method that converts a Seq[Option[T]] to Seq[T]?

Tags:

option

scala

Is there a Scala API method to convert a Seq[Option[T]] -> Seq[T]?

You can do this manually via:

seq.filter(_.isDefined).map(_.get) 

Wondering if there is a method that does the above in the general API.

like image 348
ssanj Avatar asked Aug 26 '10 04:08

ssanj


1 Answers

Absolutely, positively not. (Not!)

scala> val so1 = List(Some(1), None, Some(2), None, Some(3)) so1: List[Option[Int]] = List(Some(1), None, Some(2), None, Some(3))  scala> so1.flatten res0: List[Int] = List(1, 2, 3) 
like image 121
Randall Schulz Avatar answered Sep 23 '22 09:09

Randall Schulz