Skip to content

Instantly share code, notes, and snippets.

@JoolsF
Created July 17, 2018 14:20
Show Gist options
  • Select an option

  • Save JoolsF/54849cf0fb4205e26b2670b6645a0fd6 to your computer and use it in GitHub Desktop.

Select an option

Save JoolsF/54849cf0fb4205e26b2670b6645a0fd6 to your computer and use it in GitHub Desktop.
Error accumulation 1 naive approach
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