pub trait Monoid: Semigroup {
// Required method
fn identity() -> Self;
}
Expand description
Monoid extends Semigroup
with the an “identity” value
that append
ing to a Semigroup will
result in the same value.
Laws
The Monoid identity value must be a no-op on either side
of an append
, e.g.
use naan::prelude::*;
assert_eq!(String::from("hello").append(String::identity()),
String::identity().append(String::from("hello")));
// Generalized:
fn assert_monoid_identity<T>(t: T)
where T: PartialEq + core::fmt::Debug + Clone + Monoid
{
assert_eq!(t.clone().append(T::identity()), t.clone());
assert_eq!(T::identity().append(t.clone()), t);
}
assert_monoid_identity(String::from("hello"));
assert_monoid_identity(vec!["hello"]);