Struct CFn

Source
pub struct CFn<A, B>(pub Box<dyn Fn(A) -> B + 'static>);
Expand description

A wrapper around BFn<A, B> (a Box<dyn Fn(A) -> B + 'static>).

This struct provides a concrete type for heap-allocated, repeatable closures, which is useful for storing them in structs or passing them as arguments where a concrete type is needed (e.g., in trait implementations like Functor for functions).

CFn stands for “Clonable Function” or “Composable Function”, though it’s not inherently Clone unless the underlying boxed closure captures only Clone data (which Box<dyn Fn> doesn’t guarantee). The primary purpose here is to provide a newtype wrapper.

§Examples

use monadify::function::CFn;

let add_one = CFn::new(|x: i32| x + 1);
assert_eq!(add_one.call(5), 6);
assert_eq!(add_one.call(10), 11); // Can be called multiple times

Tuple Fields§

§0: Box<dyn Fn(A) -> B + 'static>

Implementations§

Source§

impl<A, B> CFn<A, B>

Source

pub fn new<F>(f: F) -> Self
where F: Fn(A) -> B + 'static,

Creates a new CFn by boxing the given closure.

§Parameters
  • f: A closure that implements Fn(A) -> B and is 'static.
§Returns

A new CFn<A, B> instance.

Source

pub fn call(&self, arg: A) -> B

Calls the wrapped closure.

This method takes &self and the argument arg by value, allowing the closure to be called multiple times.

§Parameters
  • arg: The argument of type A to pass to the closure.
§Returns

The result of type B from calling the closure.

Trait Implementations§

Source§

impl<A: 'static, B: 'static> Choice<A, B> for CFn<A, B>

CFn<A, B> as a Choice profunctor.

Source§

fn left<C>(self) -> Self::Pro<Result<C, A>, Result<C, B>>

If self is f: A -> B, left returns Result<C,A> -> Result<C,B>. If input is Ok(c), it remains Ok(c). If input is Err(a), it becomes Err(f(a)).

Source§

fn right<C>(self) -> Self::Pro<Result<A, C>, Result<B, C>>

If self is f: A -> B, right returns Result<A,C> -> Result<B,C>. If input is Ok(a), it becomes Ok(f(a)). If input is Err(c), it remains Err(c).

Source§

impl<A, B> Deref for CFn<A, B>

Allows CFn<A, B> to be dereferenced to &Box<dyn Fn(A) -> B + 'static>. This enables calling the boxed closure directly using (*cfn_instance)(arg) syntax if desired, though cfn_instance.call(arg) is generally preferred for clarity.

Source§

type Target = Box<dyn Fn(A) -> B>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<B, C> Profunctor<B, C> for CFn<B, C>

CFn<B, C> (a boxed function B -> C) as a Profunctor.

dimap on h: CFn<B,C> with f: A->B and g: C->D results in a new CFn<A,D> that computes a -> g(h(f(a))).

Source§

type Pro<T, U> = CFn<T, U>

The associated type representing the structure of the Profunctor. For example, if Self is CFn<B, C>, then Pro<T, U> would be CFn<T, U>.
Source§

fn dimap<A, D, A2B, C2D>(self, a2b: A2B, c2d: C2D) -> Self::Pro<A, D>
where A2B: Fn(A) -> B + 'static, C2D: Fn(C) -> D + 'static, C: 'static, B: 'static, A: 'static, D: 'static,

Maps the input and output of the profunctor. Read more
Source§

impl<A: 'static, B: 'static, C: 'static> Shl<CFn<A, B>> for CFn<B, C>

Implements g << f (backward composition) for CFn. (self << rhs)(x) is equivalent to self(rhs(x)). CFn<B,C> << CFn<A,B> results in CFn<A,C>.

Source§

type Output = CFn<A, C>

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: CFn<A, B>) -> Self::Output

Performs the << operation. Read more
Source§

impl<A: 'static, B: 'static, C: 'static> Shr<CFn<B, C>> for CFn<A, B>

Implements f >> g (forward composition) for CFn. (self >> rhs)(x) is equivalent to rhs(self(x)). CFn<A,B> >> CFn<B,C> results in CFn<A,C>.

Source§

type Output = CFn<A, C>

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: CFn<B, C>) -> Self::Output

Performs the >> operation. Read more
Source§

impl<A: 'static, B: 'static> Strong<A, B> for CFn<A, B>

CFn<A, B> as a Strong profunctor.

Source§

fn first<C: 'static>(self) -> Self::Pro<(A, C), (B, C)>

If self is f: A -> B, first returns ((A,C)) -> (B,C) where the new function is (a,c) -> (f(a), c).

Source§

fn second<C: 'static>(self) -> Self::Pro<(C, A), (C, B)>

If self is f: A -> B, second returns ((C,A)) -> (C,B) where the new function is (c,a) -> (c, f(a)).

Auto Trait Implementations§

§

impl<A, B> Freeze for CFn<A, B>

§

impl<A, B> !RefUnwindSafe for CFn<A, B>

§

impl<A, B> !Send for CFn<A, B>

§

impl<A, B> !Sync for CFn<A, B>

§

impl<A, B> Unpin for CFn<A, B>

§

impl<A, B> !UnwindSafe for CFn<A, B>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.