Trait naan::semigroup::Monoid

source ·
pub trait Monoid: Semigroup {
    // Required method
    fn identity() -> Self;
}
Expand description

Monoid extends Semigroup with the an “identity” value that appending 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"]);

Required Methods§

source

fn identity() -> Self

See Monoid

Implementations on Foreign Types§

source§

impl<A> Monoid for Option<A>where A: Semigroup,

source§

fn identity() -> Self

source§

impl<K, A> Monoid for HashMap<K, A>where K: Eq + Hash,

source§

fn identity() -> Self

source§

impl<A> Monoid for Vec<A>

source§

fn identity() -> Self

source§

impl Monoid for String

source§

fn identity() -> Self

source§

impl<K, A> Monoid for BTreeMap<K, A>where K: Ord,

source§

fn identity() -> Self

source§

impl<A, const N: usize> Monoid for ArrayVec<[Option<A>; N]>

source§

fn identity() -> Self

Implementors§

source§

impl<T> Monoid for Id<T>where T: Monoid,