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