Skip to main content

Functor

Trait Functor 

Source
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§

Source

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 self a b. Functor self => (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§

Source§

impl Functor for CatListBrand

Source§

impl Functor for IdentityBrand

Source§

impl Functor for OptionBrand

Source§

impl Functor for ThunkBrand

Source§

impl Functor for VecBrand

Source§

impl<A: 'static> Functor for TryThunkWithOkBrand<A>

Source§

impl<DoneType: 'static> Functor for StepWithDoneBrand<DoneType>

Source§

impl<E: 'static> Functor for ResultWithErrBrand<E>

Source§

impl<E: 'static> Functor for TryThunkWithErrBrand<E>

Source§

impl<First: 'static> Functor for PairWithFirstBrand<First>

Source§

impl<LoopType: 'static> Functor for StepWithLoopBrand<LoopType>

Source§

impl<Second: 'static> Functor for PairWithSecondBrand<Second>

Source§

impl<T: 'static> Functor for ResultWithOkBrand<T>