pub trait GroupByIteratorExtended: Iterator {
// Provided method
fn group_by<K, F>(self, key: F) -> GroupByIterator<Self, F, K>
where Self: Sized,
F: FnMut(&Self::Item) -> K,
K: PartialEq + Clone,
Self::Item: Clone { ... }
}
Provided Methods§
sourcefn group_by<K, F>(self, key: F) -> GroupByIterator<Self, F, K>
fn group_by<K, F>(self, key: F) -> GroupByIterator<Self, F, K>
Return an Iterator
that groups iterator elements. Consecutive elements that map to the
same key are assigned to the same group.
The returned Iterator item is (K, impl Iterator)
, where Iterator are the items of the
group.
use tantivy_common::GroupByIteratorExtended;
// group data into blocks of larger than zero or not.
let data: Vec<i32> = vec![1, 3, -2, -2, 1, 0, 1, 2];
// groups: |---->|------>|--------->|
let mut data_grouped = Vec::new();
// Note: group is an iterator
for (key, group) in data.into_iter().group_by(|val| *val >= 0) {
data_grouped.push((key, group.collect()));
}
assert_eq!(data_grouped, vec![(true, vec![1, 3]), (false, vec![-2, -2]), (true, vec![1, 0, 1, 2])]);