Skip to main content

map

Function map 

Source
pub fn map<'a, FA, A: 'a, B: 'a, Marker>(
    f: impl FunctorDispatch<'a, <FA as InferableBrand_cdc7cd43dac7585f>::Brand, A, B, FA, Marker>,
    fa: FA,
) -> <<FA as InferableBrand_cdc7cd43dac7585f>::Brand as Kind_cdc7cd43dac7585f>::Of<'a, B>
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 InferableBrand. Both owned and borrowed containers are supported:

  • Owned: map(|x: i32| x + 1, Some(5)) infers OptionBrand.
  • Borrowed: map(|x: &i32| *x + 1, &Some(5)) infers OptionBrand via the blanket impl InferableBrand for &T.

For types with multiple brands (e.g., Result), use explicit::map with a turbofish.

§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.
  • Marker: Dispatch marker type, inferred automatically.

§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]);