Skip to content

Instantly share code, notes, and snippets.

@puffnfresh
Forked from tonymorris/Applicative.scala
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save puffnfresh/4cff77dab300d9a84f8f to your computer and use it in GitHub Desktop.
Save puffnfresh/4cff77dab300d9a84f8f to your computer and use it in GitHub Desktop.
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