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 invariant docs #3492

Merged
merged 3 commits into from
Jul 3, 2020
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
73 changes: 62 additions & 11 deletions core/src/main/scala/cats/Invariant.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import scala.annotation.implicitNotFound
* {{{
* scala> import cats.implicits._
* scala> import scala.concurrent.duration._
*
* scala> val durSemigroup: Semigroup[FiniteDuration] =
* | Invariant[Semigroup].imap(Semigroup[Long])(Duration.fromNanos)(_.toNanos)
* scala> durSemigroup.combine(2.seconds, 3.seconds)
Expand All @@ -31,22 +32,72 @@ import scala.annotation.implicitNotFound
*/
def imap[A, B](fa: F[A])(f: A => B)(g: B => A): F[B]

/**
* Compose Invariant `F[_]` and `G[_]` then produce `Invariant[F[G[_]]]` using their `imap`.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import scala.concurrent.duration._
*
* scala> val durSemigroupList: Semigroup[List[FiniteDuration]] =
* | Invariant[Semigroup].compose[List].imap(Semigroup[List[Long]])(Duration.fromNanos)(_.toNanos)
* scala> durSemigroupList.combine(List(2.seconds, 3.seconds), List(4.seconds))
* res1: List[FiniteDuration] = List(2 seconds, 3 seconds, 4 seconds)
* }}}
*/
def compose[G[_]: Invariant]: Invariant[λ[α => F[G[α]]]] =
new ComposedInvariant[F, G] {
val F = self
val G = Invariant[G]
val F: Invariant[F] = self
val G: Invariant[G] = Invariant[G]
}

/**
* Compose Invariant `F[_]` and Functor `G[_]` then produce `Invariant[F[G[_]]]`
* using F's `imap` and G's `map`.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import scala.concurrent.duration._
*
* scala> val durSemigroupList: Semigroup[List[FiniteDuration]] =
* | Invariant[Semigroup]
* | .composeFunctor[List]
* | .imap(Semigroup[List[Long]])(Duration.fromNanos)(_.toNanos)
* scala> durSemigroupList.combine(List(2.seconds, 3.seconds), List(4.seconds))
* res1: List[FiniteDuration] = List(2 seconds, 3 seconds, 4 seconds)
* }}}
*/
def composeFunctor[G[_]: Functor]: Invariant[λ[α => F[G[α]]]] =
new ComposedInvariantCovariant[F, G] {
val F = self
val G = Functor[G]
val F: Invariant[F] = self
val G: Functor[G] = Functor[G]
}

/**
* Compose Invariant `F[_]` and Contravariant `G[_]` then produce `Invariant[F[G[_]]]`
* using F's `imap` and G's `contramap`.
*
* Example:
* {{{
* scala> import cats.implicits._
* scala> import scala.concurrent.duration._
*
* scala> type ToInt[T] = T => Int
* scala> val durSemigroupToInt: Semigroup[ToInt[FiniteDuration]] =
* | Invariant[Semigroup]
* | .composeContravariant[ToInt]
* | .imap(Semigroup[ToInt[Long]])(Duration.fromNanos)(_.toNanos)
* // semantically equal to (2.seconds.toSeconds.toInt + 1) + (2.seconds.toSeconds.toInt * 2) = 7
* scala> durSemigroupToInt.combine(_.toSeconds.toInt + 1, _.toSeconds.toInt * 2)(2.seconds)
* res1: Int = 7
* }}}
*/
def composeContravariant[G[_]: Contravariant]: Invariant[λ[α => F[G[α]]]] =
new ComposedInvariantContravariant[F, G] {
val F = self
val G = Contravariant[G]
val F: Invariant[F] = self
val G: Contravariant[G] = Contravariant[G]
}
}

Expand Down Expand Up @@ -104,7 +155,7 @@ object Invariant extends ScalaVersionSpecificInvariantInstances with InvariantIn

def imap[A, B](fa: Monoid[A])(f: A => B)(g: B => A): Monoid[B] =
new Monoid[B] {
val empty = f(fa.empty)
val empty: B = f(fa.empty)
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y)))
override def combineAllOption(bs: IterableOnce[B]): Option[B] =
fa.combineAllOption(bs.iterator.map(g)).map(f)
Expand Down Expand Up @@ -137,7 +188,7 @@ object Invariant extends ScalaVersionSpecificInvariantInstances with InvariantIn

def imap[A, B](fa: CommutativeMonoid[A])(f: A => B)(g: B => A): CommutativeMonoid[B] =
new CommutativeMonoid[B] {
val empty = f(fa.empty)
val empty: B = f(fa.empty)
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y)))
override def combineAllOption(bs: IterableOnce[B]): Option[B] =
fa.combineAllOption(bs.iterator.map(g)).map(f)
Expand All @@ -149,7 +200,7 @@ object Invariant extends ScalaVersionSpecificInvariantInstances with InvariantIn

def imap[A, B](fa: BoundedSemilattice[A])(f: A => B)(g: B => A): BoundedSemilattice[B] =
new BoundedSemilattice[B] {
val empty = f(fa.empty)
val empty: B = f(fa.empty)
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y)))
override def combineAllOption(bs: IterableOnce[B]): Option[B] =
fa.combineAllOption(bs.iterator.map(g)).map(f)
Expand All @@ -161,7 +212,7 @@ object Invariant extends ScalaVersionSpecificInvariantInstances with InvariantIn

def imap[A, B](fa: Group[A])(f: A => B)(g: B => A): Group[B] =
new Group[B] {
val empty = f(fa.empty)
val empty: B = f(fa.empty)
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y)))
def inverse(b: B): B = f(fa.inverse(g(b)))
override def combineAllOption(bs: IterableOnce[B]): Option[B] =
Expand All @@ -174,7 +225,7 @@ object Invariant extends ScalaVersionSpecificInvariantInstances with InvariantIn

def imap[A, B](fa: CommutativeGroup[A])(f: A => B)(g: B => A): CommutativeGroup[B] =
new CommutativeGroup[B] {
val empty = f(fa.empty)
val empty: B = f(fa.empty)
def combine(x: B, y: B): B = f(fa.combine(g(x), g(y)))
def inverse(b: B): B = f(fa.inverse(g(b)))
override def combineAllOption(bs: IterableOnce[B]): Option[B] =
Expand Down