pub trait Functor: Kind_cdc7cd43dac7585f {
// Required method
fn map<'a, A: 'a, B: 'a, Func>(
f: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>
where Func: Fn(A) -> B + 'a;
}Expand description
A type class for types that can be mapped over.
A Functor represents a context or container that allows functions to be applied
to values within that context without altering the structure of the context itself.
§Laws
Functor instances must satisfy the following laws:
- Identity:
map(identity, fa) = fa. - Composition:
map(compose(f, g), fa) = map(f, map(g, fa)).
Required Methods§
Sourcefn map<'a, A: 'a, B: 'a, Func>(
f: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>where
Func: Fn(A) -> B + 'a,
fn map<'a, A: 'a, B: 'a, Func>(
f: Func,
fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>,
) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>where
Func: Fn(A) -> B + 'a,
Maps a function over the values in the functor context.
This method applies a function to the value(s) inside the functor context, producing a new functor context with the transformed value(s).
§Type Signature
forall A B. (A -> B, Self A) -> Self B
§Type Parameters
'a: The lifetime of the values.A: The type of the value(s) inside the functor.B: The type of the result(s) of applying the function.Func: The type of the function to apply.
§Parameters
f: The function to apply to the value(s) inside the functor.fa: The functor instance containing the value(s).
§Returns
A new functor instance containing the result(s) of applying the function.
§Examples
use fp_library::{brands::*, functions::*};
let x = Some(5);
let y = map::<OptionBrand, _, _, _>(|i| i * 2, x);
assert_eq!(y, Some(10));Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.
Implementors§
impl Functor for CatListBrand
impl Functor for IdentityBrand
impl Functor for OptionBrand
impl Functor for ThunkBrand
impl Functor for Tuple1Brand
impl Functor for VecBrand
impl<A: 'static> Functor for TryThunkWithOkBrand<A>
§Type Parameters
A: The success type.
impl<DoneType: 'static> Functor for StepWithDoneBrand<DoneType>
§Type Parameters
DoneType: The done type.
impl<E: 'static> Functor for ResultWithErrBrand<E>
§Type Parameters
E: The error type.
impl<E: 'static> Functor for TryThunkWithErrBrand<E>
§Type Parameters
E: The error type.
impl<First: 'static> Functor for PairWithFirstBrand<First>
§Type Parameters
First: The type of the first value in the pair.
impl<First: 'static> Functor for Tuple2WithFirstBrand<First>
§Type Parameters
First: The type of the first value in the tuple.
impl<LoopType: 'static> Functor for StepWithLoopBrand<LoopType>
§Type Parameters
LoopType: The loop type.
impl<Second: 'static> Functor for PairWithSecondBrand<Second>
§Type Parameters
Second: The type of the second value in the pair.
impl<Second: 'static> Functor for Tuple2WithSecondBrand<Second>
§Type Parameters
Second: The type of the second value in the tuple.
impl<T: 'static> Functor for ResultWithOkBrand<T>
§Type Parameters
T: The success type.