Created
July 17, 2018 14:20
-
-
Save JoolsF/54849cf0fb4205e26b2670b6645a0fd6 to your computer and use it in GitHub Desktop.
Error accumulation 1 naive approach
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
| private def accumulateErrors[A,B](list: List[A], f: A => B): Either[List[Throwable], List[B]] = { | |
| val res = list.foldLeft(List[Error](), List[B]()) { case ((accError, accRes), item) => | |
| Try(f(item)).toEither match { | |
| case Right(i) => (accError, accRes :+ i) | |
| case Left(e) => (accError :+ new Error(e), accRes) | |
| } | |
| } | |
| if(res._1.isEmpty) Right(res._2) else Left(res._1) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment