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§
Trait Implementations§
Source§impl<A: 'static, B: 'static> Choice<A, B> for CFn<A, B>
CFn<A, B>
as a Choice
profunctor.
impl<A: 'static, B: 'static> Choice<A, B> for CFn<A, B>
CFn<A, B>
as a Choice
profunctor.
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.
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§impl<B, C> Profunctor<B, C> for CFn<B, C>
CFn<B, C>
(a boxed function B -> C
) as a Profunctor
.
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§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>
.
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§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>
.
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>
.