Scala Contramap
Aug 10, 2019 · 1 minute read · CommentsContramap 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] = Ordering.by(_.amount)
Money(300) < Money(200)