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)
-
A mathematician might point out that it might be more correct to represent the "identity" operation using the
Zero
trait rather thanDefault
, as the documentation forZero
notes that "[t]his trait is intended for use in conjunction withAdd
, as an identity". However, theZero
trait is marked as unstable, so it would only be useable on nightly Rust, and its use is deprecated. Thus,Default
. ↩
Provided Methods
fn accumulate<F>(xs: F) -> Self where
F: Iterator<Item = Self>,
Self: Sized,
F: Iterator<Item = Self>,
Self: Sized,