Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
是否有转换 Scala API 方法Seq[Option[T]] -> Seq[T]?
Seq[Option[T]] -> Seq[T]
您可以通过以下方式手动执行此操作:
seq.filter(_.isDefined).map(_.get)
想知道在通用 API 中是否有执行上述操作的方法。
绝对,绝对不是。(不是!)
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)