pub struct Endofunction<'a, FnBrand: ClonableFn, A>(pub <FnBrand as ClonableFn>::Of<'a, A, A>);Expand description
A wrapper for endofunctions (functions from a set to the same set) that enables monoidal operations.
Endofunction a represents a function a -> a.
It exists to provide a monoid instance where:
- The binary operation append is function composition.
- The identity element empty is the identity function.
The wrapped function can be accessed directly via the .0 field.
Tuple Fields§
§0: <FnBrand as ClonableFn>::Of<'a, A, A>Implementations§
Source§impl<'a, FnBrand: ClonableFn, A> Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: ClonableFn, A> Endofunction<'a, FnBrand, A>
Sourcepub fn new(f: <FnBrand as ClonableFn>::Of<'a, A, A>) -> Self
pub fn new(f: <FnBrand as ClonableFn>::Of<'a, A, A>) -> Self
Creates a new Endofunction.
This function wraps a function a -> a in an Endofunction struct.
§Type Signature
forall a. (a -> a) -> Endofunction a
§Type Parameters
FnBrand: The brand of the function (e.g.,RcFnBrand).A: The input and output type of the function.
§Parameters
f: The function to wrap.
§Returns
A new Endofunction.
§Examples
use fp_library::types::endofunction::Endofunction;
use fp_library::brands::RcFnBrand;
use fp_library::classes::clonable_fn::ClonableFn;
let f = Endofunction::<RcFnBrand, _>::new(<RcFnBrand as ClonableFn>::new(|x: i32| x * 2));
assert_eq!(f.0(5), 10);Trait Implementations§
Source§impl<'a, FnBrand: ClonableFn, A> Clone for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: ClonableFn, A> Clone for Endofunction<'a, FnBrand, A>
Source§impl<'a, FnBrand: ClonableFn, A> Debug for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: ClonableFn, A> Debug for Endofunction<'a, FnBrand, A>
Source§impl<'a, FnBrand: ClonableFn, A> Hash for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: ClonableFn, A> Hash for Endofunction<'a, FnBrand, A>
Source§impl<'a, FnBrand: 'a + ClonableFn, A: 'a> Monoid for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: 'a + ClonableFn, A: 'a> Monoid for Endofunction<'a, FnBrand, A>
Source§fn empty() -> Self
fn empty() -> Self
The identity element.
This method returns the identity endofunction, which wraps the identity function.
§Type Signature
forall a. Monoid (Endofunction a) => () -> Endofunction a
§Returns
The identity endofunction.
§Examples
use fp_library::types::endofunction::Endofunction;
use fp_library::brands::RcFnBrand;
use fp_library::classes::monoid::Monoid;
let id = Endofunction::<RcFnBrand, i32>::empty();
assert_eq!(id.0(5), 5);Source§impl<'a, FnBrand: ClonableFn, A> Ord for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: ClonableFn, A> Ord for Endofunction<'a, FnBrand, A>
Source§impl<'a, FnBrand: ClonableFn, A> PartialEq for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: ClonableFn, A> PartialEq for Endofunction<'a, FnBrand, A>
Source§impl<'a, FnBrand: ClonableFn, A> PartialOrd for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: ClonableFn, A> PartialOrd for Endofunction<'a, FnBrand, A>
Source§impl<'a, FnBrand: 'a + ClonableFn, A: 'a> Semigroup for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand: 'a + ClonableFn, A: 'a> Semigroup for Endofunction<'a, FnBrand, A>
Source§fn append(a: Self, b: Self) -> Self
fn append(a: Self, b: Self) -> Self
The result of combining the two values using the semigroup operation.
This method composes two endofunctions into a single endofunction.
Note that Endofunction composition is reversed relative to standard function composition:
append(f, g) results in f . g (read as “f after g”), meaning g is applied first, then f.
§Type Signature
forall a. Semigroup (Endofunction a) => (Endofunction a, Endofunction a) -> Endofunction a
§Parameters
a: The second function to apply (the outer function).b: The first function to apply (the inner function).
§Returns
The composed function a . b.
§Examples
use fp_library::types::endofunction::Endofunction;
use fp_library::brands::RcFnBrand;
use fp_library::classes::clonable_fn::ClonableFn;
use fp_library::classes::semigroup::Semigroup;
let f = Endofunction::<RcFnBrand, _>::new(<RcFnBrand as ClonableFn>::new(|x: i32| x * 2));
let g = Endofunction::<RcFnBrand, _>::new(<RcFnBrand as ClonableFn>::new(|x: i32| x + 1));
// f(g(x)) = (x + 1) * 2
let h = Semigroup::append(f, g);
assert_eq!(h.0(5), 12);impl<'a, FnBrand: ClonableFn, A> Eq for Endofunction<'a, FnBrand, A>
Auto Trait Implementations§
impl<'a, FnBrand, A> Freeze for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand, A> RefUnwindSafe for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand, A> Send for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand, A> Sync for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand, A> Unpin for Endofunction<'a, FnBrand, A>
impl<'a, FnBrand, A> UnwindSafe for Endofunction<'a, FnBrand, A>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more