Created
September 12, 2012 13:50
-
-
Save julienrf/3706730 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
| def identity[F[_], A](fa: F[A], functor: Functor[F]): Boolean = | |
| fa == functor.fmap((a: A) => a)(fa) |
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
| val optionFunctor2 = new Functor[Option] { | |
| def fmap[A, B](f: A => B) = _ => None | |
| } |
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) |
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[_]](fc: F[Contact])(implicit functor: Functor[F]): F[JsObject] = | |
| functor.fmap(toJson)(fc) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment