Created
February 25, 2014 20:38
-
-
Save justjoheinz/9217194 to your computer and use it in GitHub Desktop.
Functor / implicit conversion
This file contains 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
package functional | |
trait Functor[F[_]] { | |
def fmap[A, B](fa: F[A])(f: A => B): F[B] | |
} | |
final class FunctorOps[F[_], A](val self: F[A])(implicit val F: Functor[F]) { | |
final def fmap[B](f: A => B) = F.fmap(self)(f) | |
} | |
trait FunctorInstances { | |
//implicit def OptionFunctor[A] : Functor[Option] = new Functor ... | |
implicit def Function1Functor[A1]: Functor[({ type L[A] = A1 => A })#L] = | |
new Functor[({ type L[A] = A1 => A })#L] { | |
def fmap[A, B](fa: A1 => A)(f: A => B): A1 => B = f compose fa | |
} | |
} | |
object functor extends FunctorInstances { | |
implicit def toFunctorOps[F[_], A](fa: F[A])(implicit F: Functor[F]): FunctorOps[F, A] = | |
new FunctorOps(fa)(F) | |
implicit def toFunctionFunctionOps = ??? | |
} | |
object Main extends App { | |
import functor._ | |
val f1 = { x: Int => x.toString() } | |
val res1 = Function1Functor.fmap(f1) { _ => 3.0 } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment