Skip to content

Instantly share code, notes, and snippets.

@ukitaka
Created February 10, 2017 02:02
Show Gist options
  • Select an option

  • Save ukitaka/7d231996cf7c278dc62bbe4338b34b4f to your computer and use it in GitHub Desktop.

Select an option

Save ukitaka/7d231996cf7c278dc62bbe4338b34b4f to your computer and use it in GitHub Desktop.
InvariantFunctor
// 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