-
-
Save gists-app-test/3706988 to your computer and use it in GitHub Desktop.
A bottom up approach to category theory: Functors (2)
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[F[_]] { | |
def fmap[A, B](f: A => B): F[A] => F[B] | |
} |
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
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)) | |
} | |
} |
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
(A => B) => F[A] => F[B] |
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
(A => B) => List[A] => List[B] | |
(A => B) => Option[A] => Option[B] |
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
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) |
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
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