Last active
December 17, 2018 13:13
-
-
Save loicdescotte/3b05300ef5fd8fccbb6a0ae059aeb455 to your computer and use it in GitHub Desktop.
Either ops to accumumate errors (and symmetric right method)
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
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