Created
September 21, 2011 23:46
-
-
Save nuttycom/1233639 to your computer and use it in GitHub Desktop.
Semigroup and monoid actions
This file contains hidden or 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
/** Semigroup Action */ | |
trait SAct[A, -B] { outer => | |
def append(a: A, b: => B): A | |
def xappend(b: B, a: => A): A = append(a, b) | |
} | |
trait SActs { | |
implicit def s2sact[A](implicit semigroup: Semigroup[A]): SAct[A, A] = new SAct[A, A] { | |
override def append(a1: A, a2: => A): A = semigroup.append(a1, a2) | |
} | |
} | |
object SAct extends SActs { | |
def append[A, B](a: A, b: => B)(implicit sa: SAct[A, B]) = sa.append(a, b) | |
implicit def a2w[A](a: A): SActW[A] = SActW(a) | |
case class SActW[A](a: A) { | |
def |+|[B](b: => B)(implicit sa: SAct[A, B]): A = sa.append(a, b) | |
} | |
} | |
/** Monoid Action */ | |
trait MAct[A, -B] extends Zero[A] with SAct[A, B] | |
trait MActs { | |
implicit def m2mact[A](implicit monoid: Monoid[A]): MAct[A, A] = new MAct[A, A] { | |
override val zero: A = monoid.zero | |
override def append(a1: A, a2: => A): A = monoid.append(a1, a2) | |
} | |
} | |
object MAct extends MActs | |
trait Actions[M[_], A] { | |
def value: M[A] | |
def asuml[B](implicit fold: Foldable[M], mact: MAct[B, A]): B = { | |
fold.foldLeft(value, mact.zero, mact.append(_: B, _: A)) | |
} | |
def asumr[B](implicit fold: Foldable[M], mact: MAct[B, A]): B = { | |
fold.foldRight(value, mact.zero, mact.xappend _) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment