Skip to content

Commit 6a69bd9

Browse files
committedMar 14, 2024·
Deprecate GroupingMap::fold_first for reduce
1 parent 45c5dec commit 6a69bd9

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed
 

‎src/grouping_map.rs

+17-8
Original file line numberDiff line numberDiff line change
@@ -220,14 +220,14 @@ where
220220
///
221221
/// let lookup = (1..=7)
222222
/// .into_grouping_map_by(|&n| n % 3)
223-
/// .fold_first(|acc, _key, val| acc + val);
223+
/// .reduce(|acc, _key, val| acc + val);
224224
///
225225
/// assert_eq!(lookup[&0], 3 + 6);
226226
/// assert_eq!(lookup[&1], 1 + 4 + 7);
227227
/// assert_eq!(lookup[&2], 2 + 5);
228228
/// assert_eq!(lookup.len(), 3);
229229
/// ```
230-
pub fn fold_first<FO>(self, mut operation: FO) -> HashMap<K, V>
230+
pub fn reduce<FO>(self, mut operation: FO) -> HashMap<K, V>
231231
where
232232
FO: FnMut(V, &K, V) -> V,
233233
{
@@ -239,6 +239,15 @@ where
239239
})
240240
}
241241

242+
/// See [`.reduce()`](GroupingMap::reduce).
243+
#[deprecated(note = "Use .reduce() instead", since = "0.13.0")]
244+
pub fn fold_first<FO>(self, operation: FO) -> HashMap<K, V>
245+
where
246+
FO: FnMut(V, &K, V) -> V,
247+
{
248+
self.reduce(operation)
249+
}
250+
242251
/// Groups elements from the `GroupingMap` source by key and collects the elements of each group in
243252
/// an instance of `C`. The iteration order is preserved when inserting elements.
244253
///
@@ -321,7 +330,7 @@ where
321330
where
322331
F: FnMut(&K, &V, &V) -> Ordering,
323332
{
324-
self.fold_first(|acc, key, val| match compare(key, &acc, &val) {
333+
self.reduce(|acc, key, val| match compare(key, &acc, &val) {
325334
Ordering::Less | Ordering::Equal => val,
326335
Ordering::Greater => acc,
327336
})
@@ -402,7 +411,7 @@ where
402411
where
403412
F: FnMut(&K, &V, &V) -> Ordering,
404413
{
405-
self.fold_first(|acc, key, val| match compare(key, &acc, &val) {
414+
self.reduce(|acc, key, val| match compare(key, &acc, &val) {
406415
Ordering::Less | Ordering::Equal => acc,
407416
Ordering::Greater => val,
408417
})
@@ -553,7 +562,7 @@ where
553562

554563
/// Groups elements from the `GroupingMap` source by key and sums them.
555564
///
556-
/// This is just a shorthand for `self.fold_first(|acc, _, val| acc + val)`.
565+
/// This is just a shorthand for `self.reduce(|acc, _, val| acc + val)`.
557566
/// It is more limited than `Iterator::sum` since it doesn't use the `Sum` trait.
558567
///
559568
/// Returns a `HashMap` associating the key of each group with the sum of that group's elements.
@@ -574,12 +583,12 @@ where
574583
where
575584
V: Add<V, Output = V>,
576585
{
577-
self.fold_first(|acc, _, val| acc + val)
586+
self.reduce(|acc, _, val| acc + val)
578587
}
579588

580589
/// Groups elements from the `GroupingMap` source by key and multiply them.
581590
///
582-
/// This is just a shorthand for `self.fold_first(|acc, _, val| acc * val)`.
591+
/// This is just a shorthand for `self.reduce(|acc, _, val| acc * val)`.
583592
/// It is more limited than `Iterator::product` since it doesn't use the `Product` trait.
584593
///
585594
/// Returns a `HashMap` associating the key of each group with the product of that group's elements.
@@ -600,6 +609,6 @@ where
600609
where
601610
V: Mul<V, Output = V>,
602611
{
603-
self.fold_first(|acc, _, val| acc * val)
612+
self.reduce(|acc, _, val| acc * val)
604613
}
605614
}

‎tests/quick.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1495,16 +1495,16 @@ quickcheck! {
14951495
}
14961496
}
14971497

1498-
fn correct_grouping_map_by_fold_first_modulo_key(a: Vec<u8>, modulo: u8) -> () {
1498+
fn correct_grouping_map_by_reduce_modulo_key(a: Vec<u8>, modulo: u8) -> () {
14991499
let modulo = if modulo == 0 { 1 } else { modulo } as u64; // Avoid `% 0`
15001500
let lookup = a.iter().map(|&b| b as u64) // Avoid overflows
15011501
.into_grouping_map_by(|i| i % modulo)
1502-
.fold_first(|acc, &key, val| {
1502+
.reduce(|acc, &key, val| {
15031503
assert!(val % modulo == key);
15041504
acc + val
15051505
});
15061506

1507-
// TODO: Swap `fold1` with stdlib's `fold_first` when it's stabilized
1507+
// TODO: Swap `fold1` with stdlib's `reduce` when it's stabilized
15081508
let group_map_lookup = a.iter()
15091509
.map(|&b| b as u64)
15101510
.map(|i| (i % modulo, i))

0 commit comments

Comments
 (0)
Please sign in to comment.