Skip to content

Instantly share code, notes, and snippets.

@NightRa
Forked from nuttycom/gist:6690987
Last active August 29, 2015 13:56
Show Gist options
  • Save NightRa/8924667 to your computer and use it in GitHub Desktop.
Save NightRa/8924667 to your computer and use it in GitHub Desktop.
trait Functor {
type M <: { type T }
def fmap[A, B](fa: M { type T = A })(f: A => B): M { type T = B }
}
implicit class EitherRightFunctor extends Functor { self =>
type L
type M = Either { type A = self.L ; type T = B } //doesn't this specify a subtype of Either, rather than Either itself?
def fmap[A0, B0](fa: M { type A = self.L ; type B = A0 })(f: A0 => B0): Either { type A = self.L ; type B = B0 } =
fa match {
case Right(a) => Right(f(a))
case Left(l) => Left(l)
}
}
// current Scala
trait Functor[M[_]] {
def fmap[A, B](fa: M[A])(f: A => B): M[B]
}
implicit def EitherRightFunctor[L,a] = new Functor[Either[L, a]] {
def fmap[A, B](fa: Either[L, A])(f: A => B): Either[L, B] =
fa match {
case Right(a) => Right(f(a))
case Left(l) => Left(l)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment