alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Contravariant.scala)

This example Scala source code file (Contravariant.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

boolean, contravariant, contravariantlaw, equal, functor, invariantfunctorlaw

The Contravariant.scala Scala example source code

package scalaz

////
/**
 * Contravariant functors.  For example, functions provide a
 * [[scalaz.Functor]] in their result type, but a
 * [[scalaz.Contravariant]] for each argument type.
 *
 * Note that the dual of a [[scalaz.Functor]] is just a [[scalaz.Functor]]
 * itself.
 *
 * Providing an instance of this is a useful alternative to marking a
 * type parameter with `-` in Scala.
 *
 * @see [[scalaz.Contravariant.ContravariantLaw]]
 */
////
trait Contravariant[F[_]] extends InvariantFunctor[F] { self =>
  ////

  /** Transform `A`.
    *
    * @note `contramap(r)(identity)` = `r`
    */
  def contramap[A, B](r: F[A])(f: B => A): F[B]

  // derived functions

  def xmap[A, B](fa: F[A], f: A => B, g: B => A): F[B] =
    contramap(fa)(g)

  /** The composition of Contravariant F and G, `[x]F[G[x]]`, is
    * covariant.
    */
  def compose[G[_]](implicit G0: Contravariant[G]): Functor[λ[α => F[G[α]]]] =
    new Functor[λ[α => F[G[α]]]] {
      def map[A, B](fa: F[G[A]])(f: A => B) =
        self.contramap(fa)(gb => G0.contramap(gb)(f))
    }

  /** The composition of Contravariant F and Functor G, `[x]F[G[x]]`,
    * is contravariant.
    */
  def icompose[G[_]](implicit G0: Functor[G]): Contravariant[λ[α => F[G[α]]]] =
    new Contravariant[λ[α => F[G[α]]]] {
      def contramap[A, B](fa: F[G[A]])(f: B => A) =
        self.contramap(fa)(G0.lift(f))
    }

  /** The product of Contravariant `F` and `G`, `[x](F[x], G[x]])`, is
    * contravariant.
    */
  def product[G[_]](implicit G0: Contravariant[G]): Contravariant[λ[α => (F[α], G[α])]] =
    new Contravariant[λ[α => (F[α], G[α])]] {
      def contramap[A, B](fa: (F[A], G[A]))(f: B => A) =
        (self.contramap(fa._1)(f), G0.contramap(fa._2)(f))
    }

  trait ContravariantLaw extends InvariantFunctorLaw {
    /** The identity function, lifted, is a no-op. */
    def identity[A](fa: F[A])(implicit FA: Equal[F[A]]): Boolean = FA.equal(contramap(fa)(x => x), fa)

    /**
     * A series of contramaps may be freely rewritten as a single
     * contramap on a composed function.
     */
    def composite[A, B, C](fa: F[A], f1: B => A, f2: C => B)(implicit FC: Equal[F[C]]): Boolean = FC.equal(contramap(contramap(fa)(f1))(f2), contramap(fa)(f1 compose f2))
  }
  def contravariantLaw = new ContravariantLaw {}

  ////
  val contravariantSyntax = new scalaz.syntax.ContravariantSyntax[F] { def F = Contravariant.this }
}

object Contravariant {
  @inline def apply[F[_]](implicit F: Contravariant[F]): Contravariant[F] = F

  ////

  ////
}

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Contravariant.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.