pub fn map<'a, FA, A: 'a, B: 'a, Brand>(
f: impl FunctorDispatch<'a, Brand, A, B, FA, <FA as InferableBrand_cdc7cd43dac7585f<'a, Brand, A>>::Marker>,
fa: FA,
) -> <Brand as Kind_cdc7cd43dac7585f>::Of<'a, B>where
Brand: Kind_cdc7cd43dac7585f,
FA: InferableBrand_cdc7cd43dac7585f<'a, Brand, A>,Expand description
Maps a function over a functor, inferring the brand from the container type.
This is the primary API for mapping. The Brand type parameter is
inferred from the concrete type of fa via the InferableBrand trait. Both
owned and borrowed containers are supported:
- Owned:
map(|x: i32| x + 1, Some(5))infersOptionBrand. - Borrowed:
map(|x: &i32| *x + 1, &Some(5))infersOptionBrandvia the blanketimpl InferableBrand for &T.
For multi-brand types (e.g., Result), the closure’s input type
disambiguates which brand applies:
map(|x: i32| x + 1, Ok::<i32, String>(5))infersResultErrAppliedBrand<String>(maps over Ok).map(|e: String| e.len(), Err::<i32, String>("hi".into()))infersResultOkAppliedBrand<i32>(maps over Err).
For diagonal cases where the closure cannot disambiguate (e.g.,
Result<T, T>), use explicit::map
with a turbofish.
Note: Pre-bound closures (let f = |x| x + 1; map(f, Ok(5))) may
lose deferred inference context for multi-brand types, because the
closure’s parameter type is committed before map can use it for brand
resolution. Annotate the closure parameter type explicitly in these
cases: let f = |x: i32| x + 1;.
§Type Signature
forall Brand A B. Functor Brand => (A -> B, Brand A) -> Brand B
§Type Parameters
'a: The lifetime of the values.FA: The container type (owned or borrowed). Brand is inferred from this.A: The type of the value(s) inside the functor.B: The type of the result(s) of applying the function.Brand: The brand, inferred via InferableBrand from FA and the closure’s input type.
§Parameters
f: The function to apply to the value(s).fa: The functor instance (owned or borrowed).
§Returns
A new functor instance containing the result(s) of applying the function.
§Examples
use fp_library::functions::*;
// Brand inferred from Option<i32>
assert_eq!(map(|x: i32| x * 2, Some(5)), Some(10));
// Brand inferred from &Vec<i32> via blanket impl
let v = vec![1, 2, 3];
assert_eq!(map(|x: &i32| *x + 10, &v), vec![11, 12, 13]);