Added into_group_map_by, into_group_map_by_fold
@@ -20,3 +20,41 @@ pub fn into_group_map<I, K, V>(iter: I) -> HashMap<K, Vec<V>>
lookup
}
+
+pub fn into_group_map_by<I, K, V>(iter: I, f: impl Fn(&V) -> K) -> HashMap<K, Vec<V>>
+ where I: Iterator<Item=V>,
+ K: Hash + Eq,
+{
+ let mut lookup = HashMap::new();
+
+ for val in iter {
+ let key = f(&val);
+ lookup.entry(key).or_insert(Vec::new()).push(val);
+ }
+
+ lookup
+}
+
+pub fn into_group_map_by_fold<I, K, V, Acc, Fold>(iter: I, f_key: impl Fn(&V) -> K,
+ init: Acc,
+ fold: Fold) -> HashMap<K, Acc>
+ where I: Iterator<Item=V>,
+ K: Hash + Eq,
+ Acc: Clone,
+ Fold : FnMut(Acc,V) -> Acc + Clone
+
+
+{
+ let mut lookup = HashMap::new();
+
+ for val in iter {
+ let key = f_key(&val);
+ lookup.entry(key).or_insert(Vec::new()).push(val);
+ }
+
+ lookup
+ .into_iter()
+ .map(move |(key,value)| (key, value.into_iter().fold(init.clone(), fold.clone())))
+ .collect()
+}
+
