Created
February 10, 2017 02:02
-
-
Save ukitaka/7d231996cf7c278dc62bbe4338b34b4f to your computer and use it in GitHub Desktop.
InvariantFunctor
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
| // see: http://comonad.com/reader/2008/rotten-bananas/ | |
| trait InvariantFunctor[F[_]] { | |
| def xmap[A, B](fa: F[A])(f: A => B, g: B => A): F[B] | |
| } | |
| implicit val optionalInvariantFunctor = new InvariantFunctor[Option] { | |
| def xmap[A, B](fa: Option[A])(f: A => B, g: B => A): Option[B] = fa match { | |
| case Some(a) => Some(f(a)) | |
| case _ => None | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment