Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created July 11, 2012 02:09
Show Gist options
  • Save tonymorris/3087504 to your computer and use it in GitHub Desktop.
Save tonymorris/3087504 to your computer and use it in GitHub Desktop.
Comonad/Monad
trait Functor[F[_]] {
def fmap[A, B](f: A => B): F[A] => F[B]
}
trait Extend[F[_]] extends Functor[F] {
// coflatmap
def extend[A, B](f: F[A] => B): F[A] => F[B]
}
trait Comonad[F[_]] extends Extend[F] {
def copoint[A](a: F[A]): A
override def fmap[A, B](f: A => B) =
extend(q => f(copoint(q)))
}
trait FlatMap[F[_]] extends Functor[F] {
def flatMap[A, B](f: A => F[B]): F[A] => F[B]
}
trait Monad[F[_]] extends FlatMap[F] {
def point[A](a: A): F[A]
override def fmap[A, B](f: A => B) =
flatMap(q => point(f(q)))
}
case class Id[A](a: A)
object Comonad {
val IdComonad: Comonad[Id] =
new Comonad[Id] {
def copoint[A](i: Id[A]) = i.a
def extend[A, B](f: Id[A] => B) =
i => Id(f(i))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment