Skip to content

Instantly share code, notes, and snippets.

@justjoheinz
Created February 25, 2014 20:38
Show Gist options
  • Save justjoheinz/9217194 to your computer and use it in GitHub Desktop.
Save justjoheinz/9217194 to your computer and use it in GitHub Desktop.
Functor / implicit conversion
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