Given a sequence of eithers Seq[Either[String,A]] with Left being an error message. I want to obtain an Either[String,Seq[A]] where I
Building on Kevin's solution, and stealing a bit from Haskell's Either type, you can create a method partitionEithers like so:
def partitionEithers[A, B](es: Seq[Either[A, B]]): (Seq[A], Seq[B]) =
es.foldRight (Seq.empty[A], Seq.empty[B]) { case (e, (as, bs)) =>
e.fold (a => (a +: as, bs), b => (as, b +: bs))
}
And use that to build your solution
def unroll[A, B](es: Seq[Either[A, B]]): Either[Seq[A], Seq[B]] = {
val (as, bs) = partitionEithers(es)
if (!as.isEmpty) Left(as) else Right(bs)
}