Scala Contramap

Contramap function have the following definition:

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

For example scala.math.Ordering have by function that is contramap, we can create Complicated orderings by use of Simple Orderings.

def by[T, S](f: T => S)(implicit ord: Ordering[S]): Ordering[T]

Create Ordering for type Money with by contramap

case class Money(amount: Int)

import scala.math.Ordered._

implicit val moneyOrdering: Ordering[Money] =

Money(300) < Money(200)
comments powered by Disqus