-
-
Save NightRa/8924667 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
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