#iterator #traits #grouping #group #user-group #ways

grouping_by

A simple library which allows the user to group an iterator by various ways

10 releases

0.2.2 Sep 22, 2021
0.2.1 Aug 5, 2020
0.1.7 Jul 15, 2020
0.1.5 Jun 30, 2020

#976 in Rust patterns

Download history 74/week @ 2024-07-21 31/week @ 2024-07-28 14/week @ 2024-08-04 9/week @ 2024-08-11 7/week @ 2024-08-18 18/week @ 2024-08-25 16/week @ 2024-09-01 16/week @ 2024-09-08 5/week @ 2024-09-15 19/week @ 2024-09-22 58/week @ 2024-09-29 30/week @ 2024-10-06 32/week @ 2024-10-13 16/week @ 2024-10-20 61/week @ 2024-10-27 233/week @ 2024-11-03

343 downloads per month

MIT license

14KB
137 lines

grouping-by

Crates.io Documentation

This small library provides users the possibility of grouping their iterators of various ways. It is still in development and therefore is not recommended for production code. There will be breaking changes constantly.

It is similar to Java Collectors.groupingBy

Example:

#[derive(Debug, PartialEq)]
struct Point {
   x: i32,
   y: i32,
}
let array: [Point; 4] = [
       Point { x: 1, y: 2 },
       Point { x: 1, y: 3 },
       Point { x: 2, y: 2 },
       Point { x: 2, y: 2 },
];

assert_eq!(
    [
        (1, vec![&Point { x: 1, y: 2 }, &Point { x: 1, y: 3 }]),
        (2, vec![&Point { x: 2, y: 2 }, &Point { x: 2, y: 2 }])
    ]
    .iter()
    .cloned()
    .collect::<HashMap<i32, Vec<&Point>>>(),
    array.iter().grouping_by(|point| point.x)
);

More advanced usage

// This returns for each year, the contract with the most days.
contracts.iter().grouping_by_max(
    |contract| contract.date.year(), // Key of HashMap
    |contract1, contract2| contract1.days.cmp(&contract2.days), // Comparator to get the max
) // Returns `HashMap<i32, Contract>`

Usage

Just import the trait (use grouping_by::GroupingBy;) into your crate and use it on your iterators.

No runtime deps