Skip to content

Commit 8b4de1f

Browse files
New GroupingMap::sum_in
1 parent 662aab1 commit 8b4de1f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/grouping_map.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ where
533533
where
534534
V: Add<V, Output = V>,
535535
{
536-
self.reduce(|acc, _, val| acc + val)
536+
self.sum_in(HashMap::new())
537537
}
538538

539539
/// Groups elements from the `GroupingMap` source by key and multiply them.
@@ -776,4 +776,14 @@ where
776776
{
777777
self.minmax_by_in(|key, v1, v2| f(key, v1).cmp(&f(key, v2)), map)
778778
}
779+
780+
/// Apply [`sum`](Self::sum) with a provided empty map
781+
/// (`BTreeMap` or `HashMap` with any hasher).
782+
pub fn sum_in<M>(self, map: M) -> M
783+
where
784+
V: Add<V, Output = V>,
785+
M: Map<Key = K, Value = V>,
786+
{
787+
self.reduce_in(|acc, _, val| acc + val, map)
788+
}
779789
}

0 commit comments

Comments
 (0)