summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2018-07-24 15:55:44 +0300
committerLars Wirzenius <liw@liw.fi>2018-07-24 15:55:44 +0300
commit5085af39dae3d3d5c4a1fe523bfe881d6df1e4b8 (patch)
treeddc0dd7dc1fdb09d4a87fe7b94dd79967447f9b2 /src/main.rs
parenta40b75ce407fb6abc724bdaac83dec718bd3e31a (diff)
downloadwordfreq-5085af39dae3d3d5c4a1fe523bfe881d6df1e4b8.tar.gz
Change: use iterators a lotHEADmaster
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs24
1 files changed, 3 insertions, 21 deletions
diff --git a/src/main.rs b/src/main.rs
index f0fd9ae..76d4568 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -21,27 +21,9 @@ fn count_words(reader: &mut io::BufReader<File>) -> io::Result<WordCounts> {
let mut counts = WordCounts::new();
for line in reader.lines() {
let line = line?;
- add_counts(&mut counts, &count_words_in_line(&line));
- }
- Ok(counts)
-}
-
-
-fn count_words_in_line(line: &str) -> WordCounts {
- let mut counts = WordCounts::new();
- let mut word = String::new();
- for c in line.chars() {
- if c.is_alphabetic() {
- word.push(c);
- } else {
- if !word.is_empty() {
- count(&mut counts, word.clone());
- word.clear();
- }
+ for w in line.split(|c: char| !c.is_alphabetic()).filter(|w| !w.is_empty()) {
+ count(&mut counts, w.to_lowercase().clone());
}
}
- if !word.is_empty() {
- count(&mut counts, word.clone());
- }
- counts
+ Ok(counts)
}