Skip to main content

iter_with_counts

Function iter_with_counts 

Source
pub fn iter_with_counts<V: PartialEq>(
    source: impl Iterator<Item = V>,
) -> impl Iterator<Item = (V, u64)>
Expand description

Transforms an iterator of values into an iterator of value-count pairs.

The pairs of type (V, u64) are such that:

  • Each pair corresponds to a grouping of the contiguous items from source that have the same value.
  • The pair’s first component is the value from source.
  • The pair’s second component is the count of items from source in the grouping.

§Example

use basic_stats::core::iter_with_counts;

fn main() {
    let dat = [1., 3., 9., 9., 10., 10., 10., 10., 20.];
    let dat_c = iter_with_counts(dat.into_iter()).collect::<Vec<_>>();
    let exp_dat_c = vec![(1., 1), (3., 1), (9., 2), (10., 4), (20., 1)];
    assert_eq!(exp_dat_c, dat_c);
    println!("success");
}
Examples found in repository?
examples/iter_with_counts.rs (line 5)
3fn main() {
4    let dat = [1., 3., 9., 9., 10., 10., 10., 10., 20.];
5    let dat_c = iter_with_counts(dat.into_iter()).collect::<Vec<_>>();
6    let exp_dat_c = vec![(1., 1), (3., 1), (9., 2), (10., 4), (20., 1)];
7    assert_eq!(exp_dat_c, dat_c);
8    println!("success");
9}