@@ -218,16 +218,11 @@ where
218
218
/// assert_eq!(lookup[&2], 2 + 5);
219
219
/// assert_eq!(lookup.len(), 3);
220
220
/// ```
221
- pub fn reduce < FO > ( self , mut operation : FO ) -> HashMap < K , V >
221
+ pub fn reduce < FO > ( self , operation : FO ) -> HashMap < K , V >
222
222
where
223
223
FO : FnMut ( V , & K , V ) -> V ,
224
224
{
225
- self . aggregate ( |acc, key, val| {
226
- Some ( match acc {
227
- Some ( acc) => operation ( acc, key, val) ,
228
- None => val,
229
- } )
230
- } )
225
+ self . reduce_in ( operation, HashMap :: new ( ) )
231
226
}
232
227
233
228
/// See [`.reduce()`](GroupingMap::reduce).
@@ -654,4 +649,22 @@ where
654
649
{
655
650
self . fold_with_in ( |_, _| init. clone ( ) , operation, map)
656
651
}
652
+
653
+ /// Apply [`reduce`](Self::reduce) with a provided empty map
654
+ /// (`BTreeMap` or `HashMap` with any hasher).
655
+ pub fn reduce_in < FO , M > ( self , mut operation : FO , map : M ) -> M
656
+ where
657
+ FO : FnMut ( V , & K , V ) -> V ,
658
+ M : Map < Key = K , Value = V > ,
659
+ {
660
+ self . aggregate_in (
661
+ |acc, key, val| {
662
+ Some ( match acc {
663
+ Some ( acc) => operation ( acc, key, val) ,
664
+ None => val,
665
+ } )
666
+ } ,
667
+ map,
668
+ )
669
+ }
657
670
}
0 commit comments