Skip to content

Instantly share code, notes, and snippets.

@loicdescotte
Last active December 17, 2018 13:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loicdescotte/3b05300ef5fd8fccbb6a0ae059aeb455 to your computer and use it in GitHub Desktop.
Save loicdescotte/3b05300ef5fd8fccbb6a0ae059aeb455 to your computer and use it in GitHub Desktop.
Either ops to accumumate errors (and symmetric right method)
import scala.util._
implicit class EitherLeftOps[L](eithers: Seq[Either[L,_]]){
def collectLefts: Seq[L] = eithers.collect {
case Left(l) => l
}
}
implicit class EitherRightOps[R](eithers: Seq[Either[_,R]]){
def collectRights: Seq[R] = eithers.collect {
case Right(r) => r
}
}
sealed trait Error
case class NumericError(message: String) extends Error
case class OtherError(message: String) extends Error
val e1 = Right(1)
val e2 = Left(NumericError("nan"))
val e3 = Left(OtherError("foo"))
val errors = Seq(e1,e2,e3).collectLefts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment