![JAR search and dependency download from the Maven repository](/logo.png)
scalaz.Monoid.scala Maven / Gradle / Ivy
The newest version!
package scalaz
/**
* A categorical monoid.
*
*
* All monoid instances must satisfy the semigroup law and 2 additional laws:
*
* - left identity
forall a. append(zero, a) == a
* - right identity
forall a. append(a, zero) == a
*
*/
trait Monoid[M] extends Zero[M] with Semigroup[M]
abstract class MonoidLow {
implicit def monoid[M](implicit s: Semigroup[M], z: Zero[M]): Monoid[M] = new Monoid[M] {
def append(s1: M, s2: => M) = s append (s1, s2)
val zero = z.zero
}
}
object Monoid extends MonoidLow {
import Semigroup._
import Zero._
implicit def EitherLeftMonoid[A, B](implicit bz: Zero[B]) = monoid[Either.LeftProjection[A, B]](EitherLeftSemigroup, EitherLeftZero[A, B](bz))
/** A monoid for sequencing Applicative effects. */
def liftMonoid[F[_], M](implicit m: Monoid[M], a: Applicative[F]): Monoid[F[M]] = new Monoid[F[M]] {
val zero: F[M] = a.pure(m.zero)
def append(x: F[M], y: => F[M]): F[M] = a.liftA2(x, y, (m1: M, m2: M) => m.append(m1, m2))
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy