pub trait Semigroup {
// Required method
fn combine(self, other: Self) -> Self;
// Provided methods
fn combine_n(self, n: u32) -> Self
where Self: Sized + Clone { ... }
fn combine_all_option<I>(iter: I) -> Option<Self>
where I: IntoIterator<Item = Self>,
Self: Sized { ... }
}
Expand description
A Semigroup is an algebraic structure consisting of a set together with an associative binary operation. A Semigroup is a Monoid without an identity element.
Required Methods§
Provided Methods§
sourcefn combine_n(self, n: u32) -> Selfwhere
Self: Sized + Clone,
fn combine_n(self, n: u32) -> Selfwhere Self: Sized + Clone,
Combine with itself n
times.
Examples
use rust2fun::prelude::*;
assert_eq!(1, 1.combine_n(0));
assert_eq!(2, 1.combine_n(1));
assert_eq!(3, 1.combine_n(2));
assert_eq!(4, Semigroup::combine_n(1, 3));
sourcefn combine_all_option<I>(iter: I) -> Option<Self>where
I: IntoIterator<Item = Self>,
Self: Sized,
fn combine_all_option<I>(iter: I) -> Option<Self>where I: IntoIterator<Item = Self>, Self: Sized,
Combine all values in the iterator and return the total. If the sequence is empty, returns None. Otherwise, returns Some(total).
Examples
use rust2fun::prelude::*;
assert_eq!(None, Semigroup::combine_all_option(Vec::<u8>::new()));
assert_eq!(Some(6), Semigroup::combine_all_option(vec![1,2,3]));
assert_eq!(
Some("heyheyhey".to_owned()),
Semigroup::combine_all_option(repeat("hey".to_owned()).take(3)));