Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def lefts[A, B](eithers : List[Either[A, B]]) : List[A] = eithers.collect { case Left(l) => l}
- def rights[A, B](eithers : List[Either[A, B]]) : List[B] = eithers.collect { case Right(r) => r}
- scala> def splitEitherList[A,B](el: List[Either[A,B]]) = {
- val (lefts, rights) = el.partition(_.isLeft)
- (lefts.map(_.left.get), rights.map(_.right.get))
- }
- splitEitherList: [A, B](el: List[Either[A,B]])(List[A], List[B])
- scala> val el : List[Either[Int, String]] = List(Left(1), Right("Success"), Left(42))
- el: List[Either[Int,String]] = List(Left(1), Right(Success), Left(42))
- scala> val (leftValues, rightValues) = splitEitherList(el)
- leftValues: List[Int] = List(1, 42)
- rightValues: List[String] = List("Success")
- val (lefts, rights) = eithers.foldRight((List[Int](), List[String]()))((e, p) => e.fold(l => (l :: p._1, p._2), r => (p._1, r :: p._2)))
- import scalaz.std.list._
- import scalaz.std.either._
- import scalaz.syntax.monadPlus._
- val el : List[Either[Int, String]] = List(Left(1), Right("Success"), Left(42))
- scala> val (lefts, rights) = el.separate
- lefts: List[Int] = List(1, 42)
- rights: List[String] = List(Success)
- val lefts = list.flatMap(_.left.toOption)
- val rights = list.flatMap(_.right.toOption)
- 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)
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement