Skip to content

Instantly share code, notes, and snippets.

@debasishg
Created July 4, 2015 20:53
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 debasishg/67b16a1aa941586b7c43 to your computer and use it in GitHub Desktop.
Save debasishg/67b16a1aa941586b7c43 to your computer and use it in GitHub Desktop.
Generalizing the definition of an F-algebra in Scala
/**
* http://stackoverflow.com/questions/16015020/what-does-coalgebra-mean-in-the-context-of-programming
**/
def op[T: Monoid](arg: \/[(T, T), T]): T = {
val m = implicitly[Monoid[T]]
arg match {
case -\/((a, b)) => m.append(a, b)
case \/-(a) => m.zero
}
}
sealed trait MonoidArgument[+T]
case class Mappend[T](t1: T, t2: T) extends MonoidArgument[T]
case object Mempty extends MonoidArgument[Nothing]
implicit def MonoidArgumentFunctor: Functor[MonoidArgument] = new Functor[MonoidArgument] {
def map[A, B](a: MonoidArgument[A])(f: A => B): MonoidArgument[B] = a match {
case Mappend(x, y) => Mappend(f(x), f(y))
case Mempty => Mempty
}
}
import scala.language.higherKinds
abstract class Algebra[F[_]: Functor, A] {
def op: F[A] => A
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment