#iter #group #iterator #key-value

iter-group

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

1 unstable release

Uses new Rust 2021

0.1.0 Sep 1, 2022

#797 in Data structures

36 downloads per month
Used in emuman

MIT/Apache

8KB
90 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());

No runtime deps