Monoid

Trait Monoid 

Source
pub trait Monoid<'a>: Semigroup<'a> {
    // Required method
    fn empty() -> Apply0<Self>;
}
Expand description

A typeclass for monoids.

Monoid extends Semigroup with an identity element. A monoid is a set equipped with an associative binary operation and an identity element.

In functional programming, monoids are useful for combining values in a consistent way, especially when accumulating results or folding collections.

§Laws

Monoid instances must satisfy the following laws:

  • Left identity: append(empty(), x) = x.
  • Right identity: append(x, empty()) = x.
  • Associativity: append(append(x, y), z) = append(x, append(y, z)).

§Examples

Common monoids include:

  • Strings with concatenation and empty string.
  • Numbers with addition and zero.
  • Numbers with multiplication and one.
  • Lists with concatenation and empty list.

Required Methods§

Source

fn empty() -> Apply0<Self>

Returns the identity element for the monoid.

§Type Signature

forall a. Monoid a => () -> a

§Returns

The identity element which, when combined with any other element using the semigroup operation, leaves the other element unchanged.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<'a> Monoid<'a> for StringBrand

Source§

impl<'a, A> Monoid<'a> for EndomorphismBrand<'a, A>

Source§

impl<'a, A> Monoid<'a> for ConcreteVecBrand<A>
where A: 'a + Clone,