Monoid

Trait Monoid 

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

A type class 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)).

Required Methods§

Source

fn empty() -> Self

Returns the identity element for the monoid.

§Type Signature

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.

Implementations on Foreign Types§

Source§

impl<'a> Monoid<'a> for String

Source§

fn empty() -> Self

§Examples
use fp_library::functions::empty;

assert_eq!(
    empty::<String>(),
    ""
);
Source§

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

Source§

fn empty() -> Self

§Examples
use fp_library::functions::empty;

assert_eq!(
    empty::<Vec<()>>(),
    []
);
Source§

impl<'b, A: 'b + Clone, B: Monoid<'b> + 'b> Monoid<'b> for Rc<dyn Fn(A) -> B + 'b>

Source§

fn empty() -> Self

Source§

impl<'b, A: 'b + Clone, B: Monoid<'b> + 'b> Monoid<'b> for Arc<dyn Fn(A) -> B + 'b>

Source§

fn empty() -> Self

Implementors§

Source§

impl<'a, CategoryBrand: 'a + Category, A> Monoid<'a> for Endomorphism<'a, CategoryBrand, A>
where Apply1L2T<'a, CategoryBrand, A, A>: Clone,

Source§

impl<'a, ClonableFnBrand: 'a + ClonableFn, A> Monoid<'a> for Endofunction<'a, ClonableFnBrand, A>