diff options
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); } } |