Functor

Trait Functor 

Source
pub trait Functor: Kind_c3c3610c70409ee6 {
    // Required method
    fn map<'a, A: 'a, B: 'a, F>(
        f: F,
        fa: <Self as Kind_c3c3610c70409ee6>::Of<'a, A>,
    ) -> <Self as Kind_c3c3610c70409ee6>::Of<'a, B>
       where F: 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, F>( f: F, fa: <Self as Kind_c3c3610c70409ee6>::Of<'a, A>, ) -> <Self as Kind_c3c3610c70409ee6>::Of<'a, B>
where F: Fn(A) -> B + 'a,

Maps a function over the values in the functor context.

§Type Signature

forall a b. Functor f => (a -> b, f a) -> f b

§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::classes::functor::Functor;
use fp_library::brands::OptionBrand;

let x = Some(5);
let y = OptionBrand::map(|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 IdentityBrand

Source§

impl Functor for OptionBrand

Source§

impl Functor for VecBrand

Source§

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

Source§

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

Source§

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

Source§

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