#key-value #collection #collect #mapping #map #pair #aggregate

aggregate-map

Collect key-values pairs into a mapping from keys to collections of values

2 stable releases

1.0.1 May 12, 2024

#1718 in Data structures

Download history 14/week @ 2024-07-29 28/week @ 2024-08-26 39/week @ 2024-09-02 47/week @ 2024-09-09 16/week @ 2024-09-16 96/week @ 2024-09-23 42/week @ 2024-09-30 46/week @ 2024-10-07 34/week @ 2024-10-14 76/week @ 2024-10-21 27/week @ 2024-10-28 20/week @ 2024-11-04

158 downloads per month

Unlicense

8KB
76 lines

aggregate-map

Easily collect a list of key-value pairs into a mapping of keys to collections of values in Rust.


lib.rs:

Collect a list of key-value pairs into a mapping of keys to collections of values.

If you have a set of data that you want to collect into a map, by default you'll only keep the last value in the data for that key. But what if you want instead to keep a collection of all the values for each key? Enter AggregateMap!

let data = [
    ("dog", "Terry"),
    ("dog", "Zamboni"),
    ("cat", "Jonathan"),
    ("dog", "Priscilla"),
];
let collected: AggregateMap<HashMap<_, Vec<_>>> = data.into_iter().collect();
let expected = HashMap::from([
    ("dog", vec!["Terry", "Zamboni", "Priscilla"]),
    ("cat", vec!["Jonathan"])
]);
assert_eq!(collected.into_inner(), expected);

AggregateMap can be used with any map type that implements this crate's [Map] trait, such as HashMap or BTreeMap.

The collection type doesn't have to be a [Vec], too, it can be anything that implements Extend and Default. For instance, here's an example with a HashSet:

let data = [
    ("dog", "Terry"),
    ("dog", "Terry"),
    ("dog", "Priscilla"),
];
let collected: AggregateMap<HashMap<_, HashSet<_>>> = data.into_iter().collect();
let expected = HashMap::from([
    ("dog", HashSet::from(["Terry", "Priscilla"])),
]);
assert_eq!(collected.into_inner(), expected);

It can even be another AggregateMap for additional levels of aggregation!

let data = [
    ("pet", ("dog", "Terry")),
    ("pet", ("dog", "Priscilla")),
    ("stray", ("cat", "Jennifer")),
    ("pet", ("cat", "Absalom")),
];
let collected: AggregateMap<HashMap<_, AggregateMap<HashMap<_, Vec<_>>>>> =
    data.into_iter().collect();
let expected = HashMap::from([
    ("pet", HashMap::from([
        ("dog", vec!["Terry", "Priscilla"]),
        ("cat", vec!["Absalom"]),
    ])),
    ("stray", HashMap::from([
        ("cat", vec!["Jennifer"]),
    ])),
]);
let collected: HashMap<_, _> = collected
    .into_inner()
    .into_iter()
    .map(|(key, map)| (key, map.into_inner()))
    .collect();
assert_eq!(collected, expected);

Dependencies

~155KB