ContravariantMonoidal

cats.ContravariantMonoidal
See theContravariantMonoidal companion object

ContravariantMonoidal functors are functors that supply a unit along the diagonal map for the contramap2 operation.

Must obey the laws defined in cats.laws.ContravariantMonoidalLaws.

Based on ekmett's contravariant library: https://hackage.haskell.org/package/contravariant-1.4/docs/Data-Functor-Contravariant-Divisible.html

Attributes

Companion
object
Source
ContravariantMonoidal.scala
Graph
Supertypes
trait Contravariant[F]
trait Invariant[F]
trait Semigroupal[F]
trait Serializable
class Object
trait Matchable
class Any
Show all

Members list

Value members

Concrete methods

def trivial[A]: F[A]

trivial produces an instance of F for any type A that is trivial with respect to contramap2 along the diagonal

trivial produces an instance of F for any type A that is trivial with respect to contramap2 along the diagonal

Attributes

Source
ContravariantMonoidal.scala

Inherited methods

def compose[G[_] : Invariant]: Invariant[[α] =>> F[G[α]]]

Compose Invariant F[_] and G[_] then produce Invariant[F[G[_]]] using their imap.

Compose Invariant F[_] and G[_] then produce Invariant[F[G[_]]] using their imap.

Example:

scala> import cats.syntax.all._
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)

Attributes

Inherited from:
Invariant
Source
Invariant.scala
def compose[G[_] : Contravariant]: Functor[[α] =>> F[G[α]]]

Attributes

Inherited from:
Contravariant
Source
Contravariant.scala
def composeApply[G[_] : Apply]: InvariantSemigroupal[[α] =>> F[G[α]]]

Attributes

Inherited from:
InvariantSemigroupal
Source
InvariantSemigroupal.scala
def composeContravariant[G[_] : Contravariant]: Invariant[[α] =>> F[G[α]]]

Compose Invariant F[_] and Contravariant G[_] then produce Invariant[F[G[_]]] using F's imap and G's contramap.

Compose Invariant F[_] and Contravariant G[_] then produce Invariant[F[G[_]]] using F's imap and G's contramap.

Example:

scala> import cats.syntax.all._
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

Attributes

Inherited from:
Invariant
Source
Invariant.scala
override def composeFunctor[G[_] : Functor]: ContravariantSemigroupal[[α] =>> F[G[α]]]

Compose Invariant F[_] and Functor G[_] then produce Invariant[F[G[_]]] using F's imap and G's map.

Compose Invariant F[_] and Functor G[_] then produce Invariant[F[G[_]]] using F's imap and G's map.

Example:

scala> import cats.syntax.all._
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)

Attributes

Definition Classes
Inherited from:
ContravariantSemigroupal
Source
ContravariantSemigroupal.scala
def contramap[A, B](fa: F[A])(f: B => A): F[B]

Attributes

Inherited from:
Contravariant
Source
Contravariant.scala
override def imap[A, B](fa: F[A])(f: A => B)(fi: B => A): F[B]

Transform an F[A] into an F[B] by providing a transformation from A to B and one from B to A.

Transform an F[A] into an F[B] by providing a transformation from A to B and one from B to A.

Example:

scala> import cats.syntax.all._
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)
res1: FiniteDuration = 5 seconds

Attributes

Definition Classes
Inherited from:
Contravariant
Source
Contravariant.scala
def liftContravariant[A, B](f: A => B): F[B] => F[A]

Attributes

Inherited from:
Contravariant
Source
Contravariant.scala
def narrow[A, B <: A](fa: F[A]): F[B]

Lifts natural subtyping contravariance of contravariant Functors.

Lifts natural subtyping contravariance of contravariant Functors. could be implemented as contramap(identity), but the Functor laws say this is equivalent

Attributes

Inherited from:
Contravariant
Source
Contravariant.scala
def point[A](a: A): F[A]

point lifts any value into a Monoidal Functor.

point lifts any value into a Monoidal Functor.

Example:

scala> import cats.syntax.all._

scala> InvariantMonoidal[Option].point(10)
res0: Option[Int] = Some(10)

Attributes

Inherited from:
InvariantMonoidal
Source
InvariantMonoidal.scala
def product[A, B](fa: F[A], fb: F[B]): F[(A, B)]

Combine an F[A] and an F[B] into an F[(A, B)] that maintains the effects of both fa and fb.

Combine an F[A] and an F[B] into an F[(A, B)] that maintains the effects of both fa and fb.

Example:

scala> import cats.syntax.all._

scala> val noneInt: Option[Int] = None
scala> val some3: Option[Int] = Some(3)
scala> val noneString: Option[String] = None
scala> val someFoo: Option[String] = Some("foo")

scala> Semigroupal[Option].product(noneInt, noneString)
res0: Option[(Int, String)] = None

scala> Semigroupal[Option].product(noneInt, someFoo)
res1: Option[(Int, String)] = None

scala> Semigroupal[Option].product(some3, noneString)
res2: Option[(Int, String)] = None

scala> Semigroupal[Option].product(some3, someFoo)
res3: Option[(Int, String)] = Some((3,foo))

Attributes

Inherited from:
Semigroupal
Source
Semigroupal.scala
def unit: F[Unit]

Attributes

Inherited from:
InvariantMonoidal
Source
InvariantMonoidal.scala