Created
April 19, 2015 15:06
-
-
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]]
This file contains 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
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