Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save polyglotpiglet/3b3d4edf0a2e33317299f13c03d46aed to your computer and use it in GitHub Desktop.

Select an option

Save polyglotpiglet/3b3d4edf0a2e33317299f13c03d46aed to your computer and use it in GitHub Desktop.
// has a contramap method
trait MyContravariantFunctor[F[_]] {
def contramap[A, B](fa: F[A], f: B => A): F[B]
}
// We can use contramap to build a show for Salary from the show for Money
import cats._
import cats.implicits._
case class Money(amount: Int)
case class Salary(size: Money)
implicit val showMoney: Show[Money] = Show.show(m => s"$$${m.amount}")
implicit val showSalary: Show[Salary] = showMoney.contramap(_.size)
println(Money(5).show)
println(Salary(Money(50)).show)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment