1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! A `Functor` with an additional index.
#[fp_macros::document_module]
mod inner {
use {
crate::classes::*,
fp_macros::*,
};
/// A `Functor` with an additional index.
///
/// A `FunctorWithIndex` is a `Functor` that also allows you to access the
/// index of each element when mapping over the structure. The index type is
/// uniquely determined by the implementing brand via the [`WithIndex`] supertype,
/// encoding the functional dependency `f -> i` from PureScript.
///
/// ### Laws
///
/// `FunctorWithIndex` instances must satisfy:
/// * Identity: `map_with_index(|_, a| a, fa) = fa`.
/// * Compatibility with Functor: `map(f, fa) = map_with_index(|_, a| f(a), fa)`.
#[document_examples]
///
/// FunctorWithIndex laws for [`Vec`]:
///
/// ```
/// use fp_library::{
/// brands::VecBrand,
/// classes::functor_with_index::FunctorWithIndex,
/// functions::explicit::*,
/// };
///
/// let xs = vec![10, 20, 30];
///
/// // Identity: map_with_index(|_, a| a, fa) = fa
/// assert_eq!(VecBrand::map_with_index(|_, a: i32| a, xs.clone()), xs,);
///
/// // Compatibility with Functor: map(f, fa) = map_with_index(|_, a| f(a), fa)
/// let f = |a: i32| a * 2;
/// assert_eq!(
/// map::<VecBrand, _, _, _, _>(f, xs.clone()),
/// VecBrand::map_with_index(|_, a| f(a), xs),
/// );
/// ```
pub trait FunctorWithIndex: Functor + WithIndex {
/// Map a function over the structure, providing the index of each element.
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the elements.",
"The type of the result."
)]
#[document_parameters(
"The function to apply to each element and its index.",
"The structure to map over."
)]
#[document_returns("The mapped structure.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::VecBrand,
/// classes::functor_with_index::FunctorWithIndex,
/// };
///
/// let result = VecBrand::map_with_index(|i, x: i32| x + i as i32, vec![10, 20, 30]);
/// assert_eq!(result, vec![10, 21, 32]);
/// ```
fn map_with_index<'a, A: 'a, B: 'a>(
f: impl Fn(Self::Index, A) -> B + 'a,
fa: Self::Of<'a, A>,
) -> Self::Of<'a, B>;
}
/// Maps a function over a structure with access to the index of each element.
///
/// Free function version that dispatches to [the type class' associated function][`FunctorWithIndex::map_with_index`].
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the structure.",
"The type of the elements.",
"The type of the result."
)]
#[document_parameters(
"The function to apply to each element and its index.",
"The structure to map over."
)]
#[document_returns("The mapped structure.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::VecBrand,
/// functions::explicit::*,
/// };
///
/// let result = map_with_index::<VecBrand, _, _, _, _>(|i, x: i32| x + i as i32, vec![10, 20, 30]);
/// assert_eq!(result, vec![10, 21, 32]);
/// ```
pub fn map_with_index<'a, Brand: FunctorWithIndex, A: 'a, B: 'a>(
f: impl Fn(Brand::Index, A) -> B + 'a,
fa: Brand::Of<'a, A>,
) -> Brand::Of<'a, B> {
Brand::map_with_index(f, fa)
}
}
pub use inner::*;