Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate GroupingMap::fold_first for reduce #902

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions src/grouping_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,14 @@
///
/// let lookup = (1..=7)
/// .into_grouping_map_by(|&n| n % 3)
/// .fold_first(|acc, _key, val| acc + val);
/// .reduce(|acc, _key, val| acc + val);
///
/// assert_eq!(lookup[&0], 3 + 6);
/// assert_eq!(lookup[&1], 1 + 4 + 7);
/// assert_eq!(lookup[&2], 2 + 5);
/// assert_eq!(lookup.len(), 3);
/// ```
pub fn fold_first<FO>(self, mut operation: FO) -> HashMap<K, V>
pub fn reduce<FO>(self, mut operation: FO) -> HashMap<K, V>
where
FO: FnMut(V, &K, V) -> V,
{
Expand All @@ -239,6 +239,15 @@
})
}

/// See [`.reduce()`](GroupingMap::reduce).
#[deprecated(note = "Use .reduce() instead", since = "0.13.0")]
pub fn fold_first<FO>(self, operation: FO) -> HashMap<K, V>
where
FO: FnMut(V, &K, V) -> V,
{
self.reduce(operation)
}

Check warning on line 249 in src/grouping_map.rs

View check run for this annotation

Codecov / codecov/patch

src/grouping_map.rs#L244-L249

Added lines #L244 - L249 were not covered by tests

/// Groups elements from the `GroupingMap` source by key and collects the elements of each group in
/// an instance of `C`. The iteration order is preserved when inserting elements.
///
Expand Down Expand Up @@ -321,7 +330,7 @@
where
F: FnMut(&K, &V, &V) -> Ordering,
{
self.fold_first(|acc, key, val| match compare(key, &acc, &val) {
self.reduce(|acc, key, val| match compare(key, &acc, &val) {
Ordering::Less | Ordering::Equal => val,
Ordering::Greater => acc,
})
Expand Down Expand Up @@ -402,7 +411,7 @@
where
F: FnMut(&K, &V, &V) -> Ordering,
{
self.fold_first(|acc, key, val| match compare(key, &acc, &val) {
self.reduce(|acc, key, val| match compare(key, &acc, &val) {
Ordering::Less | Ordering::Equal => acc,
Ordering::Greater => val,
})
Expand Down Expand Up @@ -553,7 +562,7 @@

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

/// Groups elements from the `GroupingMap` source by key and multiply them.
///
/// This is just a shorthand for `self.fold_first(|acc, _, val| acc * val)`.
/// This is just a shorthand for `self.reduce(|acc, _, val| acc * val)`.
/// It is more limited than `Iterator::product` since it doesn't use the `Product` trait.
///
/// Returns a `HashMap` associating the key of each group with the product of that group's elements.
Expand All @@ -600,6 +609,6 @@
where
V: Mul<V, Output = V>,
{
self.fold_first(|acc, _, val| acc * val)
self.reduce(|acc, _, val| acc * val)
}
}
6 changes: 3 additions & 3 deletions tests/quick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1495,16 +1495,16 @@ quickcheck! {
}
}

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

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