Skip to content

Instantly share code, notes, and snippets.

@guilleiguaran
Last active August 29, 2015 14:17
Show Gist options
  • Save guilleiguaran/adebb06981ab055e8493 to your computer and use it in GitHub Desktop.
Save guilleiguaran/adebb06981ab055e8493 to your computer and use it in GitHub Desktop.
trait Semigroup[A] {
def op(a1: A, a2: A): A
}
trait Monoid[A] extends Semigroup[A] {
def zero: A
}
trait Group[A] extends Monoid[A] {
// Override one or both
def inverse(a1: A): A = minus(zero, v)
def minus(l: A, r: A): A = op(l, inverse(r))
}
def stringConcatMonoid = new Monoid[String] {
def op(a1: String, a2: String) = a1 + a2
val zero = ""
}
def listConcatMonoid[A] = new Monoid[List[A]] {
def op(a1: List[A], a2: List[A]) = a1 ++ a2
val zero = Nil
}
def intSum = new Group[Int] {
def op(a1: Int, a2: Int) = a1 + a2
def inverse(a: Int) = -a
val zero = 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment