Created
April 6, 2020 20:08
-
-
Save polyglotpiglet/3b3d4edf0a2e33317299f13c03d46aed to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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