Last active
November 3, 2016 14:21
-
-
Save atamborrino/84f5b359950f1db6d9a8a2080c345389 to your computer and use it in GitHub Desktop.
mapPartition
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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