[][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: FnMut(&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: FnMut(&Self::GItem) -> K,
        K: Eq + Hash
;
fn counter<K, F>(self, key: F) -> HashMap<K, usize>
    where
        K: Eq + Hash,
        F: FnMut(&Self::GItem) -> K
;
fn grouping_by_max<K, F, C>(
        self,
        key: F,
        comparator: C
    ) -> HashMap<K, Self::GItem>
    where
        K: Eq + Hash,
        F: FnMut(&Self::GItem) -> K,
        C: FnMut(&Self::GItem, &Self::GItem) -> Ordering
;
fn grouping_by_min<K, F, C>(
        self,
        key: F,
        comparator: C
    ) -> HashMap<K, Self::GItem>
    where
        K: Eq + Hash,
        F: FnMut(&Self::GItem) -> K,
        C: FnMut(&Self::GItem, &Self::GItem) -> Ordering
;
fn summing<K, V, F, G>(self, key: F, sum_func: G) -> HashMap<K, V>
    where
        K: Eq + Hash,
        F: FnMut(&Self::GItem) -> K,
        G: FnMut(&Self::GItem) -> V,
        V: Default + AddAssign
; }

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: FnMut(&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: FnMut(&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: FnMut(&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_max<K, F, C>(
    self,
    key: F,
    comparator: C
) -> HashMap<K, Self::GItem> where
    K: Eq + Hash,
    F: FnMut(&Self::GItem) -> K,
    C: FnMut(&Self::GItem, &Self::GItem) -> Ordering

Given a functions F and C 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

Example:


#[derive(Debug, Clone, PartialEq)]
struct Vector {
    x: i32,
    y: i32,
    z: i32
}

const BAR: [Vector; 4] = [
    Vector { x: 1, y: 2, z: 4 },
    Vector { x: 1, y: 3, z: 3 },
    Vector { x: 2, y: 2, z: 2 },
    Vector { x: 2, y: 2, z: 1 },
];

// Return a HashMap with the `y` fields as keys
// and the `z` fields of the vectors with that key with the maximum `x`

let a = BAR.iter().grouping_by_max(
    |vector| vector.y,
    |vector1, vector2| vector1.x.cmp(&vector2.x)
);
assert_eq!(a, [(3, &Vector { x: 1, y: 3, z: 3 } ), (2, &Vector { x: 2, y: 2, z: 2 })].iter().cloned().collect())

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

Given a functions F, C and 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

Example:


#[derive(Debug, Clone, PartialEq)]
struct Vector {
    x: i32,
    y: i32,
    z: i32
}

const BAR: [Vector; 4] = [
    Vector { x: 1, y: 2, z: 4 },
    Vector { x: 1, y: 3, z: 3 },
    Vector { x: 2, y: 2, z: 2 },
    Vector { x: 2, y: 2, z: 1 },
];

// Return a HashMap with the `y` fields as keys
// and the `z` fields of the vectors with that key with the minimum `x`

let a = BAR.iter().grouping_by_min(
    |vector| vector.y,
    |vector1, vector2| vector1.x.cmp(&vector2.x),
);
assert_eq!(a, [(3, &Vector { x: 1, y: 3, z: 3 } ), (2, &Vector { x: 1, y: 2, z: 4 })].iter().cloned().collect())

fn summing<K, V, F, G>(self, key: F, sum_func: G) -> HashMap<K, V> where
    K: Eq + Hash,
    F: FnMut(&Self::GItem) -> K,
    G: FnMut(&Self::GItem) -> V,
    V: Default + AddAssign

Return a map containing the sum of the values of a given key both obtained by provided as input functions.

Params:

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

value -> function to get the values to sum

Example:

struct Vector {
    x: i32,
    y: i32,
    z: i32
}

const BAR: [Vector; 4] = [
    Vector { x: 1, y: 2, z: 4 },
    Vector { x: 1, y: 3, z: 3 },
    Vector { x: 2, y: 2, z: 2 },
    Vector { x: 2, y: 2, z: 1 },
];

let a = BAR.iter().summing(
    |vector| vector.x,
    |vector| vector.y
);
assert_eq!(a, [(2, 4), (1, 5)].iter().cloned().collect())
Loading content...

Implementors

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

type GItem = T::Item

Loading content...