How to convert List[Either[String, Int]]
to Either[List[String], List[Int]]
using a method similar to cats sequence? For example, xs.sequence
in the following code
import cats.implicits._
val xs: List[Either[String, Int]] = List(Left("error1"), Left("error2"))
xs.sequence
returns Left(error1)
instead of required Left(List(error1, error2))
.
KevinWrights' answer suggests
val lefts = xs collect {case Left(x) => x }
def rights = xs collect {case Right(x) => x}
if(lefts.isEmpty) Right(rights) else Left(lefts)
which does return Left(List(error1, error2))
, however does cats provide out-of-the-box sequencing which would collect all the lefts?