@@ -150,15 +150,12 @@ where
150150 /// assert_eq!(lookup[&2].acc, 2 + 5);
151151 /// assert_eq!(lookup.len(), 3);
152152 /// ```
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 >
154154 where
155155 FI : FnMut ( & K , & V ) -> R ,
156156 FO : FnMut ( R , & K , V ) -> R ,
157157 {
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 ( ) )
162159 }
163160
164161 /// Groups elements from the `GroupingMap` source by key and applies `operation` to the elements
@@ -629,4 +626,21 @@ where
629626
630627 map
631628 }
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+ }
632646}
0 commit comments