Semigroup

Trait Semigroup 

Source
pub trait Semigroup {
    // Required method
    fn append<'a, ClonableFnBrand: 'a + ClonableFn>(
        a: Self,
    ) -> ApplyFn<'a, ClonableFnBrand, Self, Self>
       where Self: Sized;
}
Expand description

A typeclass for semigroups.

A Semigroup is a set equipped with an associative binary operation. This means for any elements a, b, and c in the set, the operation satisfies: (a <> b) <> c = a <> (b <> c).

In functional programming, semigroups are useful for combining values in a consistent way. They form the basis for more complex structures like monoids.

§Laws

Semigroup instances must satisfy the associative law:

  • Associativity: append(append(x)(y))(z) = append(x)(append(y)(z)).

§Examples

Common semigroups include:

  • Strings with concatenation.
  • Numbers with addition.
  • Numbers with multiplication.
  • Lists with concatenation.

Required Methods§

Source

fn append<'a, ClonableFnBrand: 'a + ClonableFn>( a: Self, ) -> ApplyFn<'a, ClonableFnBrand, Self, Self>
where Self: Sized,

Associative operation that combines two values of the same type.

§Type Signature

forall a. Semigroup a => a -> a -> a

§Parameters
  • a: First value to combine.
  • b: Second value to combine.
§Returns

The result of combining the two values using the semigroup operation.

Implementations on Foreign Types§

Source§

impl Semigroup for String

Source§

fn append<'a, ClonableFnBrand: 'a + ClonableFn>( a: Self, ) -> ApplyFn<'a, ClonableFnBrand, Self, Self>
where Self: Sized,

§Examples
use fp_library::{brands::RcFnBrand, functions::append};
use std::rc::Rc;

assert_eq!(
    append::<RcFnBrand, String>("Hello, ".to_string())("World!".to_string()),
    "Hello, World!"
);
Source§

impl<A> Semigroup for Vec<A>
where A: Clone,

Source§

fn append<'a, ClonableFnBrand: 'a + ClonableFn>( a: Self, ) -> ApplyFn<'a, ClonableFnBrand, Self, Self>
where Self: Sized,

§Examples
use fp_library::{brands::RcFnBrand, functions::append};

assert_eq!(
    append::<RcFnBrand, Vec<_>>(vec![true])(vec![false]),
    vec![true, false]
);

Implementors§

Source§

impl<'b, ClonableFnBrandSelf: ClonableFn + 'b, A: 'b> Semigroup for Endomorphism<'b, ClonableFnBrandSelf, A>