CommutativeMonoid

Trait CommutativeMonoid 

Source
pub trait CommutativeMonoid: Monoid { }
Expand description

A commutative monoid: a monoid where combine is commutative.

Laws (not enforced by type system):

  • Associative: a.combine(b).combine(c) == a.combine(b.combine(c))
  • Commutative: a.combine(b) == b.combine(a)
  • Identity: a.combine(empty()) == a == empty().combine(a)

§Example

use algebra_core::{Semigroup, Monoid, CommutativeMonoid};

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct Sum(i32);

impl Semigroup for Sum {
    fn combine(&self, other: &Self) -> Self {
        Sum(self.0 + other.0)
    }
}

impl Monoid for Sum {
    fn empty() -> Self {
        Sum(0)
    }
}

impl CommutativeMonoid for Sum {}

let x = Sum(3);
let y = Sum(5);
assert_eq!(x.combine(&y), y.combine(&x)); // commutative

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 CommutativeMonoid for ()

Source§

impl<A> CommutativeMonoid for (A,)

Source§

impl<A, B> CommutativeMonoid for (A, B)

Source§

impl<A, B, C> CommutativeMonoid for (A, B, C)

Source§

impl<A, B, C, D> CommutativeMonoid for (A, B, C, D)

Source§

impl<M: CommutativeMonoid + Clone> CommutativeMonoid for Option<M>

Source§

impl<T: Eq + Hash + Clone> CommutativeMonoid for HashSet<T>

Source§

impl<T: Ord + Clone> CommutativeMonoid for BTreeSet<T>

Implementors§

Source§

impl<T: Add<Output = T> + Clone + Zero> CommutativeMonoid for Sum<T>

Source§

impl<T: Mul<Output = T> + Clone + One> CommutativeMonoid for Product<T>