#iterator #key-value #collection #grouping #group #mapping #extend

iter-group

Library for grouping (key,value) iterators into maps of collections

3 releases (breaking)

0.3.0 Aug 8, 2024
0.2.0 Mar 24, 2023
0.1.0 Sep 1, 2022

#669 in Rust patterns

46 downloads per month
Used in emuman

MIT/Apache

14KB
189 lines

iter-group

A trivial library to extend (key, value) iterators with a method that returns a mapping type whose keys are grouped into some collection of type value.

Essentially, it replaces this:

let a = [(1, 'a'), (2, 'b'), (3, 'c'), (2, 'd'), (1, 'e'), (1, 'f')];
let mut map: HashMap<_, Vec<_>> = HashMap::default();
for (k, v) in a.into_iter() {
    map.entry(k).or_default().push(v);
}

with this:

use iter_group:::IntoGroup;
let a = [(1, 'a'), (2, 'b'), (3, 'c'), (2, 'd'), (1, 'e'), (1, 'f')];
let map = a.into_iter().group::<HashMap<_, Vec<_>>>();

See the documentation for additional examples.


lib.rs:

Extends iterators for grouping their values into a mapping type whose values are a collection.

Example 1

use std::collections::{BTreeMap, HashMap};
use iter_group::IntoGroup;

let v = vec![(1, 'a'), (2, 'b'), (3, 'c'), (2, 'd'), (1, 'e'), (1, 'f')];

let h = v.iter().cloned().group::<HashMap<_, Vec<_>>>();
assert_eq!(h.get(&1), Some(&vec!['a', 'e', 'f']));
assert_eq!(h.get(&2), Some(&vec!['b', 'd']));
assert_eq!(h.get(&3), Some(&vec!['c']));

let b = v.iter().cloned().group::<BTreeMap<_, Vec<_>>>();
assert_eq!(b.get(&1), Some(&vec!['a', 'e', 'f']));
assert_eq!(b.get(&2), Some(&vec!['b', 'd']));
assert_eq!(b.get(&3), Some(&vec!['c']));

Example 2

use std::collections::{BTreeMap, HashMap};
use iter_group::IntoGroup;

let v = vec![Some((1, 'a')), Some((2, 'b')), Some((1, 'c'))];

let h = v.iter().cloned().group::<Option<HashMap<_, Vec<_>>>>().unwrap();
assert_eq!(h.get(&1), Some(&vec!['a', 'c']));
assert_eq!(h.get(&2), Some(&vec!['b']));

let b = v.iter().cloned().group::<Option<BTreeMap<_, Vec<_>>>>().unwrap();
assert_eq!(b.get(&1), Some(&vec!['a', 'c']));
assert_eq!(b.get(&2), Some(&vec!['b']));

let v = vec![Some((1, 'a')), None, Some((2, 'b'))];
assert!(v.iter().cloned().group::<Option<HashMap<_, Vec<_>>>>().is_none());
assert!(v.iter().cloned().group::<Option<BTreeMap<_, Vec<_>>>>().is_none());

Example 3

use std::collections::{BTreeMap, HashMap};
use iter_group::IntoGroup;

let v = vec![Ok((1, 'a')), Ok((2, 'b')), Ok((1, 'c'))];

let h = v.iter().cloned().group::<Result<HashMap<_, Vec<_>>, ()>>().unwrap();
assert_eq!(h.get(&1), Some(&vec!['a', 'c']));
assert_eq!(h.get(&2), Some(&vec!['b']));

let b = v.iter().cloned().group::<Result<BTreeMap<_, Vec<_>>, ()>>().unwrap();
assert_eq!(b.get(&1), Some(&vec!['a', 'c']));
assert_eq!(b.get(&2), Some(&vec!['b']));

let v = vec![Ok((1, 'a')), Err(()), Ok((2, 'b'))];
assert!(v.iter().cloned().group::<Result<HashMap<_, Vec<_>>, ()>>().is_err());
assert!(v.iter().cloned().group::<Result<BTreeMap<_, Vec<_>>, ()>>().is_err());

Also offers the option to extend iterators for summarizing their values into a mapping type whose values are a sum.

Example 1

use std::collections::{BTreeMap, HashMap};
use iter_group::IntoSummary;

let v = vec![('a', 1), ('b', 1), ('a', 2), ('b', 2), ('a', 3)];

let h = v.iter().cloned().summarize::<HashMap<_, _>>();
assert_eq!(h.get(&'a'), Some(&6));
assert_eq!(h.get(&'b'), Some(&3));

let b = v.iter().cloned().summarize::<BTreeMap<_, _>>();
assert_eq!(b.get(&'a'), Some(&6));
assert_eq!(b.get(&'b'), Some(&3));

Example 2

use std::collections::{BTreeMap, HashMap};
use iter_group::IntoSummary;

let v = vec![Some(('a', 1)), Some(('b', 2)), Some(('a', 3))];

let h = v.iter().cloned().summarize::<Option<HashMap<_, _>>>().unwrap();
assert_eq!(h.get(&'a'), Some(&4));
assert_eq!(h.get(&'b'), Some(&2));

let b = v.iter().cloned().summarize::<Option<BTreeMap<_, _>>>().unwrap();
assert_eq!(b.get(&'a'), Some(&4));
assert_eq!(b.get(&'b'), Some(&2));

let v = vec![Some(('a', 1)), None, Some(('b', 2))];
assert!(v.iter().cloned().summarize::<Option<HashMap<_, _>>>().is_none());
assert!(v.iter().cloned().summarize::<Option<BTreeMap<_, _>>>().is_none());

Example 3

use std::collections::{BTreeMap, HashMap};
use iter_group::IntoSummary;

let v = vec![Ok(('a', 1)), Ok(('b', 2)), Ok(('a', 3))];

let h = v.iter().cloned().summarize::<Result<HashMap<_, _>, ()>>().unwrap();
assert_eq!(h.get(&'a'), Some(&4));
assert_eq!(h.get(&'b'), Some(&2));

let b = v.iter().cloned().summarize::<Result<BTreeMap<_, _>, ()>>().unwrap();
assert_eq!(b.get(&'a'), Some(&4));
assert_eq!(b.get(&'b'), Some(&2));

let v = vec![Ok(('a', 1)), Err(()), Ok(('b', 2))];
assert!(v.iter().cloned().summarize::<Result<HashMap<_, _>, ()>>().is_err());
assert!(v.iter().cloned().summarize::<Result<BTreeMap<_, _>, ()>>().is_err());

No runtime deps