Counter
Simple counter library for Rust iterables. Inspired by, and largely mimicing the API of, Python's Counter.
Too tired for proper documentation at the moment; see the source. It's not really that long.
Examples
Get the most common characters in a string, breaking ties alphabetically
most_common_ordered()
uses the natural ordering of keys which are Ord
.
let by_common = init
.most_common_ordered
.;
let expected = vec!;
assert!;
Get the most common characters in a string, using your own ordering
For example, here we break ties reverse alphabetically.
let counter = init;
let by_common = counter.most_common_tiebreaker.;
let expected = vec!;
assert!;
Directly modify the backing map
This is backed by a map to which you have full access:
let mut counter = init;
counter.map.remove;
assert!;
Count any iterable which is Hash + Eq
You can't use the most_common*
functions unless T is also Clone
, but simple counting works fine on a minimal data type.
// <https://en.wikipedia.org/wiki/867-5309/Jenny>
let intys = vec!;
let inty_counts = init;
println!;
// {Inty { i: 8 }: 2, Inty { i: 0 }: 3, Inty { i: 9 }: 1, Inty { i: 3 }: 1,
// Inty { i: 7 }: 1, Inty { i: 6 }: 1, Inty { i: 5 }: 1}
assert!;
assert!;
assert!;