@@ -218,16 +218,11 @@ where
218218 /// assert_eq!(lookup[&2], 2 + 5);
219219 /// assert_eq!(lookup.len(), 3);
220220 /// ```
221- pub fn reduce < FO > ( self , mut operation : FO ) -> HashMap < K , V >
221+ pub fn reduce < FO > ( self , operation : FO ) -> HashMap < K , V >
222222 where
223223 FO : FnMut ( V , & K , V ) -> V ,
224224 {
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 ( ) )
231226 }
232227
233228 /// See [`.reduce()`](GroupingMap::reduce).
@@ -654,4 +649,22 @@ where
654649 {
655650 self . fold_with_in ( |_, _| init. clone ( ) , operation, map)
656651 }
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+ }
657670}
0 commit comments