Skip to content

Instantly share code, notes, and snippets.

@gists-app-test
Forked from julienrf/Functor.scala
Created September 12, 2012 14:31
Show Gist options
  • Save gists-app-test/3706988 to your computer and use it in GitHub Desktop.
Save gists-app-test/3706988 to your computer and use it in GitHub Desktop.
A bottom up approach to category theory: Functors (2)
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
implicit val listFunctor = new Functor[List] {
def fmap[A, B](f: A => B): List[A] => List[B] = _ match {
case Nil => Nil
case a :: as => f(a) :: fmap(f)(as)
}
}
val optionFunctor = new Functor[Option] {
def fmap[A, B](f: A => B): Option[A] => Option[B] = _ match {
case None => None
case Some(a) => Some(f(a))
}
}
(A => B) => F[A] => F[B]
(A => B) => List[A] => List[B]
(A => B) => Option[A] => Option[B]
implicit val listFunctor = new Functor[List] { … }
implicit val optionFunctor = new Functor[Option] { … }
def toJsonF[F[_]](fc: F[Contact])(implicit functor: Functor[F]): F[JsObject] =
functor.fmap(toJson)(fc)
def toJsonF[F[_]](functor: Functor[F]): F[Contact] => F[JsObject] =
functor.fmap(toJson)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment