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)); // commutativeDyn 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.