Skip to main content

GroupBy

Trait GroupBy 

Source
pub trait GroupBy<V> {
    // Required method
    fn group_by<K: Ord>(self, key: impl FnMut(&V) -> K) -> BTreeMap<K, Vec<V>>;
}
Expand description

イテレータのグルーピング。

§Suggestions

BTreeMapHashMap 用で分けるべき? メソッド名を冗長にしたくない。

§See also

UsizeGroupBy

Required Methods§

Source

fn group_by<K: Ord>(self, key: impl FnMut(&V) -> K) -> BTreeMap<K, Vec<V>>

§Examples
use nekolib::traits::GroupBy;

let a = vec![1, 4, 3, -5, -6, 0, 2, -2, 3];
let g1 = a.iter().copied().group_by(|&ai: &i32| ai.rem_euclid(3));
let g2 = a.iter().copied().group_by(|&ai: &i32| ai % 3);

assert_eq!(g1.len(), 3);
assert_eq!(g1[&0], [3, -6, 0, 3]);
assert_eq!(g1[&1], [1, 4, -5, -2]);
assert_eq!(g1[&2], [2]);

assert_eq!(g2.len(), 4);
assert_eq!(g2[&-2], [-5, -2]);
assert_eq!(g2[&0], [3, -6, 0, 3]);
assert_eq!(g2[&1], [1, 4]);
assert_eq!(g2[&2], [2]);

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<V, I: Iterator<Item = V>> GroupBy<V> for I