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

# Scala example source code file (InvariantFunctor.scala)

This example Scala source code file (InvariantFunctor.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.

## Java - Scala tags/keywords

bijection, boolean, equal, invariantfunctor, invariantfunctorlaw, isomorphism

## The InvariantFunctor.scala Scala example source code

```package scalaz

////
/**
* Unary type constructor that supports an `xmap` operation that converts an `F[A]` to an `F[B]` given
* two functions, `A => B` and `B => A`.
*
* An invariant functor must satisfy two laws:
*  - identity - xmap(ma)(identity, identity) == ma
*  - composite - xmap(xmap(ma, f1, g1), f2, g2) == xmap(ma, f2 compose f1, g1, compose g2)
*
* Also known as an exponential functor.
*
*
* @see [[scalaz.InvariantFunctor.InvariantFunctorLaw]]
*/
////
trait InvariantFunctor[F[_]]  { self =>
////

import BijectionT.Bijection
import Isomorphism.<=>

/** Converts `ma` to a value of type `F[B]` using the provided functions `f` and `g`. */
def xmap[A, B](ma: F[A], f: A => B, g: B => A): F[B]

/** Converts `ma` to a value of type `F[B]` using the provided bijection. */
def xmapb[A, B](ma: F[A])(b: Bijection[A, B]): F[B] = xmap(ma, b.to, b.from)

/** Converts `ma` to a value of type `F[B]` using the provided isomorphism. */
def xmapi[A, B](ma: F[A])(iso: A <=> B): F[B] = xmap(ma, iso.to, iso.from)

trait InvariantFunctorLaw {

def invariantIdentity[A](fa: F[A])(implicit FA: Equal[F[A]]): Boolean =
FA.equal(xmap[A, A](fa, x => x, x => x), fa)

def invariantComposite[A, B, C](fa: F[A], f1: A => B, g1: B => A, f2: B => C, g2: C => B)(implicit FC: Equal[F[C]]): Boolean =
FC.equal(xmap(xmap(fa, f1, g1), f2, g2), xmap(fa, f2 compose f1, g1 compose g2))
}

def invariantFunctorLaw = new InvariantFunctorLaw {}
////
val invariantFunctorSyntax = new scalaz.syntax.InvariantFunctorSyntax[F] { def F = InvariantFunctor.this }
}

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

////
////
}
```

## Other Scala examples (source code examples)

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

 ... this post is sponsored by my books ... #1 New Release! FP Best Seller