I want to split a List[Either[A, B]] in two lists.
Is there a better way ?
def lefts[A, B](eithers : List[Either[A, B]]) : List[A] = eit
Well, in case it doesn't have to be a one-liner... then it can be a no-brainer.
def split[A,B](eithers : List[Either[A, B]]):(List[A],List[B]) = {
val lefts = scala.collection.mutable.ListBuffer[A]()
val rights = scala.collection.mutable.ListBuffer[B]()
eithers.map {
case Left(l) => lefts += l
case Right(r) => rights += r
}
(lefts.toList, rights.toList)
}
But, to be honest, I'd prefer Marth's answer :)