cats.arrow.Compose.scala Maven / Gradle / Ivy
The newest version!
package cats
package arrow
import simulacrum.typeclass
/**
* Must obey the laws defined in cats.laws.ComposeLaws.
*
* Here's how you can use `>>>` and `<<<`
* Example:
* {{{
* scala> import cats.implicits._
* scala> val f : Int => Int = (_ + 1)
* scala> val g : Int => Int = (_ * 100)
* scala> (f >>> g)(3)
* res0: Int = 400
* scala> (f <<< g)(3)
* res1: Int = 301
* }}}
*/
@typeclass trait Compose[F[_, _]] { self =>
@simulacrum.op("<<<", alias = true)
def compose[A, B, C](f: F[B, C], g: F[A, B]): F[A, C]
@simulacrum.op(">>>", alias = true)
def andThen[A, B, C](f: F[A, B], g: F[B, C]): F[A, C] =
compose(g, f)
def algebraK: SemigroupK[λ[α => F[α, α]]] =
new SemigroupK[λ[α => F[α, α]]] {
def combineK[A](f1: F[A, A], f2: F[A, A]): F[A, A] = self.compose(f1, f2)
}
def algebra[A]: Semigroup[F[A, A]] =
new Semigroup[F[A, A]] {
def combine(f1: F[A, A], f2: F[A, A]): F[A, A] = self.compose(f1, f2)
}
}
object Compose {
implicit val catsInstancesForFunction1: ArrowChoice[Function1] with CommutativeArrow[Function1] =
cats.instances.function.catsStdInstancesForFunction1
implicit val catsComposeForMap: Compose[Map] = cats.instances.map.catsStdComposeForMap
}