diff options
author | Lars Wirzenius <liw@liw.fi> | 2018-07-24 15:55:44 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2018-07-24 15:55:44 +0300 |
commit | 5085af39dae3d3d5c4a1fe523bfe881d6df1e4b8 (patch) | |
tree | ddc0dd7dc1fdb09d4a87fe7b94dd79967447f9b2 /src/counts.rs | |
parent | a40b75ce407fb6abc724bdaac83dec718bd3e31a (diff) | |
download | wordfreq-5085af39dae3d3d5c4a1fe523bfe881d6df1e4b8.tar.gz |
Diffstat (limited to 'src/counts.rs')
-rw-r--r-- | src/counts.rs | 19 |
1 files changed, 3 insertions, 16 deletions
diff --git a/src/counts.rs b/src/counts.rs index a339eb3..b7c4352 100644 --- a/src/counts.rs +++ b/src/counts.rs @@ -5,28 +5,15 @@ pub fn count(counts: &mut WordCounts, word: String) { add(counts, word, 1); } -pub fn add_counts(counts: &mut WordCounts, other: &WordCounts) { - for (word, count) in other.iter() { - add(counts, word.to_string(), *count); - } -} - pub fn add(counts: &mut WordCounts, word: String, count: u32) { let counter = counts.entry(word).or_insert(0); *counter += count; } pub fn print(counts: &WordCounts, max: usize) { - let mut top = Vec::new(); - for (word, count) in counts.iter() { - top.push((count, word)); - if top.len() > max { - top.sort(); - top.reverse(); - top.truncate(max); - } - } - for (count, word) in top.iter() { + let mut counts: Vec<_> = counts.iter().map(|(w,c)| (c,w)).collect(); + counts.sort(); + for (count, word) in counts.into_iter().rev().take(max) { println!("{} {}", count, word); } } |