Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Abstract out DisjunctionT? #7

Open
adelbertc opened this issue Feb 27, 2015 · 0 comments
Open

Abstract out DisjunctionT? #7

adelbertc opened this issue Feb 27, 2015 · 0 comments

Comments

@adelbertc
Copy link
Owner

I'll just leave this here..

/** Laws? */
trait SumElim[F[_, _], G[_], A] {
  def elim[B, C](f: F[A, B])(left: A => C, right: B => C): G[C]
}

object SumElim {
  implicit def either[A]: SumElim[Either, Id, A] =
    new SumElim[Either, Id, A] {
      def elim[B, C](f: Either[A, B])(left: A => C, right: B => C): C = f.fold(left, right)
    }

  implicit def disjunction[A]: SumElim[Disjunction, Id, A] =
    new SumElim[Disjunction, Id, A] {
      def elim[B, C](f: Disjunction[A, B])(left: A => C, right: B => C): C = f.fold(left, right)
    }

  implicit def disjunctionT[G[_] : Functor, A]: SumElim[DisjunctionT[G, ?, ?], G, A] =
    new SumElim[DisjunctionT[G, ?, ?], G, A] {
      def elim[B, C](f: DisjunctionT[G, A, B])(left: A => C, right: B => C): G[C] = f.fold(left, right)
    }

  implicit val future: SumElim[({type l[e, a] = Future[a]})#l, Future, Throwable] =
    new SumElim[({type l[e, a] = Future[a]})#l, Future, Throwable] {
      def elim[B, C](f: Future[B])(left: Throwable => C, right: B => C): Future[C] =
        f.map(right).recover(left)
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant