Skip to content

Instantly share code, notes, and snippets.

@tonymorris
Created July 11, 2012 01:24
Show Gist options
  • Save tonymorris/3087315 to your computer and use it in GitHub Desktop.
Save tonymorris/3087315 to your computer and use it in GitHub Desktop.
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)))
}
@tonymorris
Copy link
Author

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)))   
}

case class Id[A](a: A)

object Comonad {
  val IdComonad: Comonad[Id] =
    // error: object creation impossible, since method extend in trait Extend of type [A, B](f: Id[A] => B)Id[A] => Id[B] is not defined
    new Comonad[Id] {
      def copoint[A](i: Id[A]) = i.a
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment