Created
July 4, 2015 20:53
-
-
Save debasishg/67b16a1aa941586b7c43 to your computer and use it in GitHub Desktop.
Generalizing the definition of an F-algebra in Scala
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
/** | |
* 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