将Eithers列表更改为scala中的两个值列表

时间:2014-07-03 14:12:58

标签: scala either

如何将Eithers列表更改为右侧和左侧两个值列表。当我使用partition时,它会返回两个不是值的列表。最简单的方法是什么?

4 个答案:

答案 0 :(得分:6)

foldLeft可让您轻松编写自己的方法:

def separateEithers[T, U](list: List[Either[T, U]]) = {
  val (ls, rs) = list.foldLeft(List[T](), List[U]()) {
    case ((ls, rs), Left(x)) => (x :: ls, rs)
    case ((ls, rs), Right(x)) => (ls, x :: rs)
  }
  (ls.reverse, rs.reverse)
}

答案 1 :(得分:2)

分区后,您必须map两个结果列表。

val origin: List[Either[A, B]] = ???
val (lefts, rights) = origin.partition(_.isInstanceOf[Left[_]])
val leftValues = lefts.map(_.asInstanceOf[Left[A]].a)
val rightValues = rights.map(_.asInstanceOf[Right[B]].b)

如果您对演员阵容和isInstanceOf不满意,您也可以两次通过:

val leftValues = origin collect {
  case Left(a) => a
}
val rightValues = origin collect {
  case Right(b) => b
}

如果你对这两个传球感到不满意,你必须“手动”做到这一点:

def myPartition[A, B](origin: List[Either[A, B]]): (List[A], List[B]) = {
  val leftBuilder = List.newBuilder[A]
  val rightBuilder = List.newBuilder[B]
  origin foreach {
    case Left(a)  => leftBuilder += a
    case Right(b) => rightBuilder += b
  }
  (leftBuilder.result(), rightBuilder.result())
}

最后,如果你不喜欢可变状态,你可以这样做:

def myPartition[A, B](origin: List[Either[A, B]]): (List[A], List[B]) = {
  @tailrec
  def loop(xs: List[Either[A, B]], accLeft: List[A],
      accRight: List[B]): (List[A], List[B]) = {
    xs match {
      case Nil            => (accLeft.reverse, accRight.reverse)
      case Left(a) :: xr  => loop(xr, a :: accLeft, accRight)
      case Right(b) :: xr => loop(xr, accLeft, b :: accRight)
    }
  }
  loop(origin, Nil, Nil)
}

答案 2 :(得分:2)

如果你通过列表​​两次通过就可以了,你可以使用collect:

type E = Either[String, Int]
val xs: List[E] = List(Left("foo"), Right(1), Left("bar"), Right(2))
val rights = xs.collect { case Right(x) => x}
// rights: List[Int] = List(1, 2)

val lefts = xs.collect { case Left(x) => x}
// lefts: List[String] = List(foo, bar)

答案 3 :(得分:1)

用于理解,像这样,

for ( Left(v) <- xs ) yield v

for ( Right(v) <- xs ) yield v