Skip to content

Instantly share code, notes, and snippets.

@atamborrino
Last active November 3, 2016 14:21
Show Gist options
  • Select an option

  • Save atamborrino/84f5b359950f1db6d9a8a2080c345389 to your computer and use it in GitHub Desktop.

Select an option

Save atamborrino/84f5b359950f1db6d9a8a2080c345389 to your computer and use it in GitHub Desktop.
mapPartition
def mapPartition[A, B, C](as: Seq[A])(f: A => Either[B, C]): (Seq[B], Seq[C]) = {
as.foldLeft((Vector.empty[B], Vector.empty[C])) { (acc, a) =>
f(a) match {
case Left(b) => (acc._1 :+ b, acc._2)
case Right(c) => (acc._1, acc._2 :+ c)
}
}
}
val seq: Seq[Either[String, Int]] = Seq(Left(1), Right("a"))
val (strings, ints): (Seq[String], Seq[Int]) = mapPartition(seq)(identity) // (Vector(1),Vector(a)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment