@@ -151,15 +151,12 @@ where
151151 /// assert_eq!(lookup[&2].acc, 2 + 5);
152152 /// assert_eq!(lookup.len(), 3);
153153 /// ```
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 >
155155 where
156156 FI : FnMut ( & K , & V ) -> R ,
157157 FO : FnMut ( R , & K , V ) -> R ,
158158 {
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 ( ) )
163160 }
164161
165162 /// Groups elements from the `GroupingMap` source by key and applies `operation` to the elements
@@ -628,4 +625,20 @@ where
628625
629626 map
630627 }
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+ }
631644}
0 commit comments