Trait an_rope::metric::Monoid [] [src]

pub trait Monoid: Add<Self, Output = Self> + Default + Sized {
    fn accumulate<F>(xs: F) -> Self
    where
        F: Iterator<Item = Self>,
        Self: Sized
, { ... } }

The class of monoids

Monoids are types with an accumulative binary operation that has an identity.

Technically, Add<Self, Output=Self> is standing in for "semigroup" here, while Default is standing in for "identity"1.

An instance M should satisfy the following laws:

  • x.add(M::default()) = x
  • M::default().add(x) = x
  • x.add(y.add(z)) = z.add(x.add(y))
  • M::accumulate(a) = a.fold(M::default,M::sum)

  1. A mathematician might point out that it might be more correct to represent the "identity" operation using the Zero trait rather than Default, as the documentation for Zero notes that "[t]his trait is intended for use in conjunction with Add, as an identity". However, the Zero trait is marked as unstable, so it would only be useable on nightly Rust, and its use is deprecated. Thus, Default

Provided Methods

Implementors