Functor

Trait Functor 

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

§Type Parameters
  • F: The type of the function to apply.
  • A: The type of the value(s) inside the functor.
  • B: The type of the result(s) of applying the function.
§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>