Skip to content

Instantly share code, notes, and snippets.

Last active August 19, 2019 18:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save taisukeoe/5b81778e582bc61f9a27a0863a1a2670 to your computer and use it in GitHub Desktop.
import cats.
import cats.implicits._
case class MyError(errorMsg: String)
object MyError {
implicit val s: Semigroup[MyError] = new cats.kernel.Semigroup[MyError] {
override def combine(t1: MyError, t2: MyError): MyError = MyError(t1.errorMsg + "\n" + t2.errorMsg)
val a: Either[MyError, String] = MyError("boom!").asLeft
val b: Either[MyError, String] = MyError("ouch!").asLeft
val list = List(a, b)
// catsEitherのApplicative合成(ap)はfail fast。最初のエラー値と共に失敗する。
// Left(MyError(boom!))
// このエラー値を蓄積する方法はないだろうか?
def meSequenceG[T[_]: Traverse, F[_], E: Semigroup, A](l: T[F[A]])(implicit ME: MonadError[F, E]): F[T[A]] =
// Left(MyError(boom!
// ouch!))
object AccumulativeEither {
def applicative[E](implicit S: Semigroup[E]): Applicative[({ type λ[AA] = Either[E, AA]})#λ] = new Applicative[({
type λ[A] = Either[E, A]
})#λ] {
override def pure[A](x: A): Either[E, A] = x.asRight
override def ap[A, B](ff: Either[E, A => B])(fa: Either[E, A]): Either[E, B] = (fa, ff) match {
case (Right(a), Right(f)) => f(a).asRight
case (Left(e1), Left(e2)) => S.combine(e2, e1).asLeft
case (Left(e), _) => e.asLeft
case (_, Left(e)) => e.asLeft
def meSequenceE[T[_], F[_], E, A](l: T[F[A]])(implicit ME: MonadError[F, E], S: Semigroup[E], TR: Traverse[T]): F[T[A]] =
// Left(MyError(boom!
// ouch!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment