-
-
Save puffnfresh/4cff77dab300d9a84f8f to your computer and use it in GitHub Desktop.
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
import scala.language.higherKinds | |
trait Functor[F[_]] { | |
def fmap[A, B](f: A => B): F[A] => F[B] | |
} | |
trait Apply[F[_]] { | |
val functor: Functor[F] | |
def ap[A, B](f: F[A => B]): F[A] => F[B] | |
def fmap[A, B](f: A => B): F[A] => F[B] = | |
functor.fmap(f) | |
} | |
trait Applicative[F[_]] { | |
val apply: Apply[F] | |
def ap[A, B](f: F[A => B]): F[A] => F[B] = | |
apply.ap(f) | |
def pure[A](a: A): F[A] | |
def functor: Functor[F] = | |
apply.functor | |
def fmap[A, B](f: A => B): F[A] => F[B] = | |
functor.fmap(f) | |
object Derived { | |
def functor: Functor[F] = | |
new Functor[F] { | |
def fmap[A, B](f: A => B): F[A] => F[B] = | |
ap(pure(f)) | |
} | |
def fmap[A, B](f: A => B): F[A] => F[B] = | |
functor.fmap(f) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment