[][src]Trait grouping_by::GroupingBy

pub trait GroupingBy {
    type GItem;
    fn grouping_by<K, F>(self, key: F) -> HashMap<K, Vec<Self::GItem>>
    where
        F: Fn(&Self::GItem) -> K,
        K: Eq + Hash
;
fn grouping_by_as_set<K, F>(
        self,
        key: F
    ) -> HashMap<K, HashSet<Self::GItem>>
    where
        Self::GItem: Eq + Hash,
        F: Fn(&Self::GItem) -> K,
        K: Eq + Hash
;
fn counter<K, F>(self, key: F) -> HashMap<K, usize>
    where
        K: Eq + Hash,
        F: Fn(&Self::GItem) -> K
;
fn grouping_by_min<K, F, G, O, C>(
        self,
        key: F,
        comparator: C,
        finisher: G
    ) -> HashMap<K, O>
    where
        K: Eq + Hash,
        F: Fn(&Self::GItem) -> K,
        G: Fn(&Self::GItem) -> O,
        C: Fn(&O, &O) -> Ordering
;
fn grouping_by_max<K, F, G, O, C>(
        self,
        key: F,
        comparator: C,
        finisher: G
    ) -> HashMap<K, O>
    where
        K: Eq + Hash,
        F: Fn(&Self::GItem) -> K,
        G: Fn(&Self::GItem) -> O,
        C: Fn(&O, &O) -> Ordering
; }

Associated Types

type GItem

The type of the Item of the iterator

Loading content...

Required methods

fn grouping_by<K, F>(self, key: F) -> HashMap<K, Vec<Self::GItem>> where
    F: Fn(&Self::GItem) -> K,
    K: Eq + Hash

Group by the key function given as parameter. The keys are the different values that the function can return, and the values are a Vec with the items of the iterator which has the key as property

Example


let numbers_grouped = [-1i8, -2, 1, 2]
    .iter()
    .grouping_by_as_set(|number| number.abs());

assert_eq!(
    numbers_grouped,
    [(1, [1, -1].iter().collect()), (2, [2, -2].iter().collect())]
        .iter()
        .cloned()
        .collect::<HashMap<i8, HashSet<&i8>>>()
);

fn grouping_by_as_set<K, F>(self, key: F) -> HashMap<K, HashSet<Self::GItem>> where
    Self::GItem: Eq + Hash,
    F: Fn(&Self::GItem) -> K,
    K: Eq + Hash

Group by the key function given as parameter. The keys are the different values that the function can return, and the values are a HashSet with the items of the iterator which has the key as property

Example


let numbers_grouped = [-1i8, -2, 1, 2]
    .iter()
    .grouping_by_as_set(|number| number.abs());

assert_eq!(
    numbers_grouped,
    [(1, [1, -1].iter().collect()), (2, [2, -2].iter().collect())]
        .iter()
        .cloned()
        .collect::<HashMap<i8, HashSet<&i8>>>()
);

fn counter<K, F>(self, key: F) -> HashMap<K, usize> where
    K: Eq + Hash,
    F: Fn(&Self::GItem) -> K, 

Count the elements of the iterator given a function

Example

let numbers_counted = [1, 2, 2, 3, 4].iter().counter(|&&x| x);

assert_eq!(
   numbers_counted,
   [(1, 1), (2, 2), (3, 1), (4, 1)]
       .iter()
       .cloned()
       .collect::<HashMap<i8, usize>>()
)

fn grouping_by_min<K, F, G, O, C>(
    self,
    key: F,
    comparator: C,
    finisher: G
) -> HashMap<K, O> where
    K: Eq + Hash,
    F: Fn(&Self::GItem) -> K,
    G: Fn(&Self::GItem) -> O,
    C: Fn(&O, &O) -> Ordering

Given a functions F, C and G, compute the minimum of the elements given a comparator and a finisher. Params:

key: F -> function to create the keys of the resulting map

comparator: C -> function to get the min value

finisher: G -> function to perform the last transformation to the value

fn grouping_by_max<K, F, G, O, C>(
    self,
    key: F,
    comparator: C,
    finisher: G
) -> HashMap<K, O> where
    K: Eq + Hash,
    F: Fn(&Self::GItem) -> K,
    G: Fn(&Self::GItem) -> O,
    C: Fn(&O, &O) -> Ordering

Given a functions F, C and G, compute the maximum of the elements given a comparator and a finisher. Params:

key -> function to create the keys of the resulting map

comparator -> function to get the max value

finisher -> function to perform the last transformation to the value

Loading content...

Implementors

impl<T: Iterator> GroupingBy for T[src]

type GItem = <T as Iterator>::Item

Loading content...