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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! A `ParFoldable` with an additional index.
#[fp_macros::document_module]
mod inner {
use {
crate::{
classes::*,
kinds::*,
},
fp_macros::*,
};
/// A `ParFoldable` with an additional index.
///
/// `ParFoldableWithIndex` is the parallel counterpart to
/// [`FoldableWithIndex`](crate::classes::FoldableWithIndex). Implementors define
/// [`par_fold_map_with_index`][ParFoldableWithIndex::par_fold_map_with_index] directly.
///
/// ### Laws
///
/// `ParFoldableWithIndex` instances must be compatible with their `ParFoldable` instance:
/// * Compatibility with `ParFoldable`:
/// `par_fold_map(f, fa) = par_fold_map_with_index(|_, a| f(a), fa)`.
///
/// ### Thread Safety
///
/// The index type must satisfy `Self::Index: Send + Sync + Copy` when calling
/// [`par_fold_map_with_index`][ParFoldableWithIndex::par_fold_map_with_index]. These bounds
/// apply even when the `rayon` feature is disabled.
#[document_examples]
///
/// ParFoldableWithIndex laws for [`Vec`]:
///
/// ```
/// use fp_library::{
/// brands::*,
/// classes::par_foldable_with_index::ParFoldableWithIndex,
/// functions::*,
/// };
///
/// let xs = vec![1, 2, 3];
/// let f = |a: i32| a.to_string();
///
/// // Compatibility with ParFoldable:
/// // par_fold_map(f, fa) = par_fold_map_with_index(|_, a| f(a), fa)
/// assert_eq!(
/// par_fold_map::<VecBrand, _, _>(f, xs.clone()),
/// VecBrand::par_fold_map_with_index(|_, a| f(a), xs),
/// );
/// ```
pub trait ParFoldableWithIndex: ParFoldable + FoldableWithIndex {
/// Maps each element and its index to a [`Monoid`] value and combines them in parallel.
///
/// When the `rayon` feature is enabled, the mapping and reduction are done across multiple
/// threads. Otherwise falls back to a sequential indexed fold.
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The type of the elements.",
"The monoid type."
)]
#[document_parameters(
"The function to apply to each element and its index. Must be `Send + Sync`.",
"The structure to fold over."
)]
#[document_returns("The combined result.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::VecBrand,
/// classes::par_foldable_with_index::ParFoldableWithIndex,
/// };
///
/// let result =
/// VecBrand::par_fold_map_with_index(|i, x: i32| format!("{i}:{x}"), vec![10, 20, 30]);
/// assert_eq!(result, "0:101:202:30");
/// ```
fn par_fold_map_with_index<'a, A: 'a + Send, M: Monoid + Send + 'a>(
f: impl Fn(Self::Index, A) -> M + Send + Sync + 'a,
fa: Apply!(<Self as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> M
where
Self::Index: Send + Sync + Copy + 'a;
}
/// Maps each element and its index to a [`Monoid`] value and combines them in parallel.
///
/// Free function version that dispatches to
/// [`ParFoldableWithIndex::par_fold_map_with_index`].
#[document_signature]
#[document_type_parameters(
"The lifetime of the values.",
"The brand of the structure.",
"The type of the elements.",
"The monoid type."
)]
#[document_parameters(
"The function to apply to each element and its index. Must be `Send + Sync`.",
"The structure to fold over."
)]
#[document_returns("The combined result.")]
#[document_examples]
///
/// ```
/// use fp_library::{
/// brands::*,
/// functions::*,
/// };
///
/// let result: String =
/// par_fold_map_with_index::<VecBrand, _, _>(|i, x: i32| format!("{i}:{x}"), vec![10, 20, 30]);
/// assert_eq!(result, "0:101:202:30");
/// ```
pub fn par_fold_map_with_index<'a, Brand, A: 'a + Send, M: Monoid + Send + 'a>(
f: impl Fn(Brand::Index, A) -> M + Send + Sync + 'a,
fa: Apply!(<Brand as Kind!( type Of<'a, T: 'a>: 'a; )>::Of<'a, A>),
) -> M
where
Brand: ParFoldableWithIndex,
Brand::Index: Send + Sync + Copy + 'a, {
Brand::par_fold_map_with_index(f, fa)
}
}
pub use inner::*;