@@ -150,15 +150,12 @@ where
150
150
/// assert_eq!(lookup[&2].acc, 2 + 5);
151
151
/// assert_eq!(lookup.len(), 3);
152
152
/// ```
153
- pub fn fold_with < FI , FO , R > ( self , mut init : FI , mut operation : FO ) -> HashMap < K , R >
153
+ pub fn fold_with < FI , FO , R > ( self , init : FI , operation : FO ) -> HashMap < K , R >
154
154
where
155
155
FI : FnMut ( & K , & V ) -> R ,
156
156
FO : FnMut ( R , & K , V ) -> R ,
157
157
{
158
- self . aggregate ( |acc, key, val| {
159
- let acc = acc. unwrap_or_else ( || init ( key, & val) ) ;
160
- Some ( operation ( acc, key, val) )
161
- } )
158
+ self . fold_with_in ( init, operation, HashMap :: new ( ) )
162
159
}
163
160
164
161
/// Groups elements from the `GroupingMap` source by key and applies `operation` to the elements
@@ -629,4 +626,21 @@ where
629
626
630
627
map
631
628
}
629
+
630
+ /// Apply [`fold_with`](Self::fold_with) with a provided empty map
631
+ /// (`BTreeMap` or `HashMap` with any hasher).
632
+ pub fn fold_with_in < FI , FO , R , M > ( self , mut init : FI , mut operation : FO , map : M ) -> M
633
+ where
634
+ FI : FnMut ( & K , & V ) -> R ,
635
+ FO : FnMut ( R , & K , V ) -> R ,
636
+ M : Map < Key = K , Value = R > ,
637
+ {
638
+ self . aggregate_in (
639
+ |acc, key, val| {
640
+ let acc = acc. unwrap_or_else ( || init ( key, & val) ) ;
641
+ Some ( operation ( acc, key, val) )
642
+ } ,
643
+ map,
644
+ )
645
+ }
632
646
}
0 commit comments