pub struct Endofunction<'a, ClonableFnBrand: ClonableFn, A: 'a>(pub ApplyFn<'a, ClonableFnBrand, 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.
§Examples
use fp_library::{
brands::{EndofunctionBrand, RcFnBrand},
functions::{append, empty},
classes::ClonableFn,
types::Endofunction,
};
use std::rc::Rc;
// Create Endofunctions
let f = Endofunction(<RcFnBrand as ClonableFn>::new(|x: i32| x * 2));
let g = Endofunction(<RcFnBrand as ClonableFn>::new(|x: i32| x + 1));
// Compose functions (f after g)
let fg = append::<RcFnBrand, EndofunctionBrand<RcFnBrand, i32>>(f)(g);
assert_eq!(fg.0(3), 8); // double(increment(3)) = 8
// Identity element
let id = empty::<EndofunctionBrand<RcFnBrand, i32>>();
assert_eq!(id.0(42), 42);
Tuple Fields§
§0: ApplyFn<'a, ClonableFnBrand, A, A>
Implementations§
Source§impl<'a, ClonableFnBrand: ClonableFn, A> Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand: ClonableFn, A> Endofunction<'a, ClonableFnBrand, A>
Trait Implementations§
Source§impl<'a, ClonableFnBrand: ClonableFn, A> Clone for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand: ClonableFn, A> Clone for Endofunction<'a, ClonableFnBrand, A>
Source§impl<'a, ClonableFnBrand: ClonableFn, A> Debug for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand: ClonableFn, A> Debug for Endofunction<'a, ClonableFnBrand, A>
Source§impl<'a, ClonableFnBrand: ClonableFn, A> Hash for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand: ClonableFn, A> Hash for Endofunction<'a, ClonableFnBrand, A>
Source§impl<'a, ClonableFnBrand: 'a + ClonableFn, A> Monoid<'a> for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand: 'a + ClonableFn, A> Monoid<'a> for Endofunction<'a, ClonableFnBrand, A>
Source§impl<'a, ClonableFnBrand: ClonableFn, A> Ord for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand: ClonableFn, A> Ord for Endofunction<'a, ClonableFnBrand, A>
Source§impl<'a, ClonableFnBrand: ClonableFn, A> PartialEq for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand: ClonableFn, A> PartialEq for Endofunction<'a, ClonableFnBrand, A>
Source§impl<'a, ClonableFnBrand: ClonableFn, A> PartialOrd for Endofunction<'a, ClonableFnBrand, A>where
ApplyFn<'a, ClonableFnBrand, A, A>: PartialOrd,
impl<'a, ClonableFnBrand: ClonableFn, A> PartialOrd for Endofunction<'a, ClonableFnBrand, A>where
ApplyFn<'a, ClonableFnBrand, A, A>: PartialOrd,
Source§impl<'b, ClonableFnBrand: 'b + ClonableFn, A> Semigroup<'b> for Endofunction<'b, ClonableFnBrand, A>
impl<'b, ClonableFnBrand: 'b + ClonableFn, A> Semigroup<'b> for Endofunction<'b, ClonableFnBrand, A>
Source§fn append<'a, CFB: 'a + 'b + ClonableFn>(
a: Self,
) -> ApplyFn<'a, CFB, Self, Self>where
Self: Sized,
'b: 'a,
fn append<'a, CFB: 'a + 'b + ClonableFn>(
a: Self,
) -> ApplyFn<'a, CFB, Self, Self>where
Self: Sized,
'b: 'a,
§Examples
use fp_library::{
brands::{EndofunctionBrand, RcFnBrand},
functions::append,
classes::ClonableFn,
types::Endofunction,
};
use std::rc::Rc;
let double = <RcFnBrand as ClonableFn>::new(|x: i32| x * 2);
let increment = <RcFnBrand as ClonableFn>::new(|x: i32| x + 1);
assert_eq!(
(append::<RcFnBrand, EndofunctionBrand<RcFnBrand, i32>>(Endofunction(double))(Endofunction(increment.clone()))).0(3),
8
);
assert_eq!(
(append::<RcFnBrand, EndofunctionBrand<RcFnBrand, i32>>(Endofunction(increment.clone()))(Endofunction(increment))).0(3),
5
);
impl<'a, ClonableFnBrand: ClonableFn, A> Eq for Endofunction<'a, ClonableFnBrand, A>
Auto Trait Implementations§
impl<'a, ClonableFnBrand, A> Freeze for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand, A> RefUnwindSafe for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand, A> Send for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand, A> Sync for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand, A> Unpin for Endofunction<'a, ClonableFnBrand, A>
impl<'a, ClonableFnBrand, A> UnwindSafe for Endofunction<'a, ClonableFnBrand, 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
Mutably borrows from an owned value. Read more