Skip to main content

Functor

Trait Functor 

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

§Hierarchy Unification

This trait now inherits from Kind_cdc7cd43dac7585f, ensuring that all functor contexts satisfy the strict lifetime requirements where the type argument must outlive the context’s application lifetime.

By explicitly requiring that the type parameter outlives the application lifetime 'a, we provide the compiler with the necessary guarantees to handle trait objects (like dyn Fn) commonly used in functor implementations. This resolves potential E0310 errors where the compiler cannot otherwise prove that captured variables in closures satisfy the required lifetime bounds.

§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: impl Fn(A) -> B + 'a, fa: <Self as Kind_cdc7cd43dac7585f>::Of<'a, A>, ) -> <Self as Kind_cdc7cd43dac7585f>::Of<'a, B>

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.
§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 Tuple1Brand

Source§

impl Functor for VecBrand

Source§

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

§Type Parameters
  • A: The success type.
Source§

impl<Brand: Bifunctor, A: 'static> Functor for BifunctorFirstAppliedBrand<Brand, A>

Maps over the first type parameter of a bifunctor by delegating to Bifunctor::bimap with identity for the second argument.

§Type Parameters
  • Brand: The bifunctor brand.
  • A: The fixed second type parameter.
Source§

impl<Brand: Bifunctor, B: 'static> Functor for BifunctorSecondAppliedBrand<Brand, B>

Maps over the second type parameter of a bifunctor by delegating to Bifunctor::bimap with identity for the first argument.

§Type Parameters
  • Brand: The bifunctor brand.
  • B: The fixed first type parameter.
Source§

impl<Brand: Profunctor, A: 'static> Functor for ProfunctorFirstAppliedBrand<Brand, A>

Maps over the second (covariant) type parameter of a profunctor via Profunctor::rmap.

§Type Parameters
  • Brand: The profunctor brand.
  • A: The fixed first type parameter.
Source§

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

§Type Parameters
  • DoneType: The done type.
Source§

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

§Type Parameters
  • E: The error type.
Source§

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

§Type Parameters
  • E: The error type.
Source§

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

§Type Parameters
  • First: The type of the first value in the pair.
Source§

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

§Type Parameters
  • First: The type of the first value in the tuple.
Source§

impl<FunctionBrand: CloneableFn + 'static, A: 'static, B: 'static> Functor for BazaarListBrand<FunctionBrand, A, B>

§Type Parameters
  • FunctionBrand: The cloneable function brand.
  • A: The focus type.
  • B: The replacement type.
Source§

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

§Type Parameters
  • LoopType: The loop type.
Source§

impl<R: 'static> Functor for ConstBrand<R>

§Type Parameters
  • R: The stored type.
Source§

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

§Type Parameters
  • Second: The type of the second value in the pair.
Source§

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

§Type Parameters
  • Second: The type of the second value in the tuple.
Source§

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

§Type Parameters
  • T: The success type.