Skip to content

Instantly share code, notes, and snippets.

@Jacoby6000
Created April 19, 2015 15:06
Show Gist options
  • Save Jacoby6000/9048856fec687f06c2b9 to your computer and use it in GitHub Desktop.
Save Jacoby6000/9048856fec687f06c2b9 to your computer and use it in GitHub Desktop.
Allows you to turn List[Either[A,B]] in to a Tuple2[List[A],List[B]]
implicit class PimpedListOfEither[A,B](list: List[Either[A,B]]) {
def tupled = {
@tailrec
def traverseList(curList: List[Either[A,B]], transformedList: (List[A], List[B]) = (List.empty[A], List.empty[B])): (List[A], List[B]) = {
traverseList(
curList.tail,
curList.headOption map {
case Left(a) => (a :: transformedList._1, transformedList._2)
case Right(b) => (transformedList._1, b :: transformedList._2)
} getOrElse transformedList)
}
traverseList(list)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment