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

Add Coproduct fold #1032

Merged
merged 1 commit into from
May 15, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion core/src/main/scala/cats/data/Coproduct.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cats
package data

import cats.arrow.NaturalTransformation
import cats.functor.Contravariant

/** `F` on the left and `G` on the right of [[Xor]].
Expand Down Expand Up @@ -57,6 +58,28 @@ final case class Coproduct[F[_], G[_], A](run: F[A] Xor G[A]) {
def toValidated: Validated[F[A], G[A]] =
run.toValidated

/**
* Fold this coproduct into a new type constructor using two natural transformations.
*
* Example:
* {{{
* scala> import cats.arrow.NaturalTransformation
* scala> import cats.data.Coproduct
* scala> val listToOption =
* | new NaturalTransformation[List, Option] {
* | def apply[A](fa: List[A]): Option[A] = fa.headOption
* | }
* scala> val optionToOption = NaturalTransformation.id[Option]
* scala> val cp1: Coproduct[List, Option, Int] = Coproduct.leftc(List(1,2,3))
* scala> val cp2: Coproduct[List, Option, Int] = Coproduct.rightc(Some(4))
* scala> cp1.fold(listToOption, optionToOption)
* res0: Option[Int] = Some(1)
* scala> cp2.fold(listToOption, optionToOption)
* res1: Option[Int] = Some(4)
* }}}
*/
def fold[H[_]](f: NaturalTransformation[F, H], g: NaturalTransformation[G, H]): H[A] =
run.fold(f.apply, g.apply)
}

object Coproduct extends CoproductInstances {
Expand All @@ -78,7 +101,6 @@ object Coproduct extends CoproductInstances {
def left[G[_]]: CoproductLeft[G] = new CoproductLeft[G]

def right[F[_]]: CoproductRight[F] = new CoproductRight[F]

}

private[data] sealed abstract class CoproductInstances3 {
Expand Down