Решение на Text Info от Иван Иванов
Резултати
- 15 точки от тестове
- 0 бонус точки
- 15 точки общо
- 15 успешни тест(а)
- 0 неуспешни тест(а)
Код
Лог от изпълнението
Compiling solution v0.1.0 (file:///tmp/d20171026-5817-dwv7ec/solution) invalid expression !2652 = !DIExpression(6, 34, 0, 6) invalid expression !2655 = !DIExpression(6, 34, 8, 6) invalid expression !2651 = !DIExpression(6, 34, 0, 6) invalid expression !2654 = !DIExpression(6, 34, 8, 6) Finished dev [unoptimized + debuginfo] target(s) in 4.33 secs Running target/debug/deps/solution-f5dd4e94aa395cae running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Running target/debug/deps/solution_test-c3b431457e2a7a27 running 15 tests test solution_test::test_alpha_count ... ok test solution_test::test_alpha_count_2 ... ok test solution_test::test_char_count ... ok test solution_test::test_cyrillic_letter_count ... ok test solution_test::test_emotions ... ok test solution_test::test_emotions_repeated_punctuation ... ok test solution_test::test_empty_string ... ok test solution_test::test_latin_letter_count ... ok test solution_test::test_sentence_count ... ok test solution_test::test_sentence_count_2 ... ok test solution_test::test_triple_dots_count ... ok test solution_test::test_unicode_char_count ... ok test solution_test::test_word_count ... ok test solution_test::test_word_count_2 ... ok test solution_test::test_word_count_3 ... ok test result: ok. 15 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out Doc-tests solution running 0 tests test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
История (5 версии и 4 коментара)
Иван качи решение на 21.10.2017 20:58 (преди почти 8 години)
#[derive(Debug)]
pub struct TextInfo {
s: String, // remove later pub
}
impl TextInfo {
pub fn new(s: &str) -> Self {
TextInfo { s: String::from(s) }
}
pub fn char_count(&self) -> usize {
self.s.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
- count_if(&self.s, &is_alpha)
+ count_if(&self.s, &|elem| elem.is_alphabetic())
}
pub fn latin_letter_count(&self) -> usize {
count_if(&self.s, &is_latin)
}
pub fn cyrillic_letter_count(&self) -> usize {
count_if(&self.s, &is_cyrillic)
}
pub fn word_count(&self) -> usize {
- self.s
- .split(is_word_separator)
- .filter(is_not_empty)
- .filter(is_valid_word)
- .count()
+ self.s.split(is_word_separator).filter(is_not_empty).count()
}
pub fn sentence_count(&self) -> usize {
self.s
.split(is_sentance_separator)
.filter(is_not_empty)
- .filter(is_valid_word)
.count()
}
pub fn emotion(&self) -> String {
let text = self.s.replace("???", "?").replace("...", ".");
let e_count = count_if(&text, &|elem| elem == '!');
let q_count = count_if(&text, &|elem| elem == '?');
let n_count = count_if(&text, &|elem| elem == '.');
- if e_count == 0 && q_count == 0 && n_count == 0 {
- String::from("")
- } else if e_count > q_count && e_count >= n_count {
+ if e_count > q_count && e_count >= n_count {
String::from("😮")
} else if q_count > e_count && q_count >= n_count {
String::from("🤔")
} else {
String::from("😐")
}
}
}
// Helper Functions
fn count_if(s: &str, fun: &Fn(char) -> bool) -> usize {
s.chars().fold(0, |counter, curr| if fun(curr) {
counter + 1
} else {
counter
})
}
-fn is_alpha(c: char) -> bool {
- c.is_alphabetic()
-}
-
fn is_latin(c: char) -> bool {
match c {
'a'...'z' | 'A'...'Z' => true,
_ => false,
}
}
fn is_cyrillic(c: char) -> bool {
match c {
'а'...'я' | 'А'...'Я' => true,
_ => false,
}
}
+fn is_letter(c: char) -> bool {
+ is_latin(c) || is_cyrillic(c)
+}
+
fn is_sentance_separator(c: char) -> bool {
c == '.' || c == '!' || c == '?'
}
fn is_word_separator(c: char) -> bool {
- !(is_latin(c) || is_cyrillic(c))
+ !is_letter(c)
}
-fn is_valid_word(s: &&str) -> bool {
- !s.is_empty() && *s != "." && *s != "!" && *s != "?"
-}
-
fn is_not_empty(s: &&str) -> bool {
!s.is_empty()
}
#[cfg(test)]
mod tests {
#[test]
fn test_basic() {
let t = ::TextInfo::new("abc. 123. абв.");
assert_eq!(14, t.char_count());
assert_eq!(6, t.alphabetic_count());
assert_eq!(3, t.cyrillic_letter_count());
assert_eq!(3, t.latin_letter_count());
assert_eq!(2, t.word_count());
assert_eq!(3, t.sentence_count());
assert_eq!("😐", t.emotion());
}
#[test]
fn test_empty_string() {
let empty_str = ::TextInfo::new("");
assert_eq!(0, empty_str.char_count());
assert_eq!(0, empty_str.alphabetic_count());
assert_eq!(0, empty_str.cyrillic_letter_count());
assert_eq!(0, empty_str.latin_letter_count());
assert_eq!(0, empty_str.sentence_count());
- assert_eq!("", empty_str.emotion());
+ assert_eq!("😐", empty_str.emotion());
}
#[test]
- fn test_edge_cases() {
+ fn test_multiple_punctoation() {
let tmp = ::TextInfo::new("Who are you???");
assert_eq!(14, tmp.char_count());
assert_eq!(1, tmp.sentence_count());
assert_eq!(0, tmp.cyrillic_letter_count());
assert_eq!("🤔", tmp.emotion())
}
#[test]
fn test_cyrillic() {
let cyr = ::TextInfo::new("Аз съм Иван!");
assert_eq!(12, cyr.char_count());
assert_eq!(9, cyr.cyrillic_letter_count());
assert_eq!("😮", cyr.emotion());
}
#[test]
fn test_count_sentences() {
let first = ::TextInfo::new("One, two, and three!");
let second = ::TextInfo::new("Huh? What's this??");
- let third = ::TextInfo::new("Hmm... Hm, Hm, Hmm.");
+ let third = ::TextInfo::new("Hmm... Hm, ... Hm, Hmm.");
let fourth = ::TextInfo::new("?...!");
let fifth = ::TextInfo::new(",?");
let sixth = ::TextInfo::new(".?");
assert_eq!(1, first.sentence_count());
assert_eq!(2, second.sentence_count());
- assert_eq!(2, third.sentence_count());
+ assert_eq!(3, third.sentence_count());
assert_eq!(0, fourth.sentence_count());
assert_eq!(1, fifth.sentence_count());
assert_eq!(0, sixth.sentence_count());
}
#[test]
fn test_count_words() {
let first = ::TextInfo::new("One, two, and three!");
let second = ::TextInfo::new("Huh? What's this??");
let third = ::TextInfo::new("Hmm... Hm, Hm, Hmm.");
let fourth = ::TextInfo::new("?...!");
let fifth = ::TextInfo::new("");
let sixth = ::TextInfo::new("Баба,дядо");
let seventh = ::TextInfo::new("~*_1-баба-x-дядо-1_*~");
assert_eq!(4, first.word_count());
assert_eq!(4, second.word_count());
assert_eq!(4, third.word_count());
assert_eq!(0, fourth.word_count());
assert_eq!(0, fifth.word_count());
assert_eq!(2, sixth.word_count());
assert_eq!(3, seventh.word_count());
}
-}
+}
Иван качи решение на 21.10.2017 21:11 (преди почти 8 години)
#[derive(Debug)]
pub struct TextInfo {
s: String, // remove later pub
}
impl TextInfo {
pub fn new(s: &str) -> Self {
TextInfo { s: String::from(s) }
}
pub fn char_count(&self) -> usize {
self.s.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
- count_if(&self.s, &|elem| elem.is_alphabetic())
+ count_chars(&self.s, &|elem| elem.is_alphabetic())
}
pub fn latin_letter_count(&self) -> usize {
- count_if(&self.s, &is_latin)
+ count_chars(&self.s, &is_latin)
}
pub fn cyrillic_letter_count(&self) -> usize {
- count_if(&self.s, &is_cyrillic)
+ count_chars(&self.s, &is_cyrillic)
}
pub fn word_count(&self) -> usize {
- self.s.split(is_word_separator).filter(is_not_empty).count()
+ count_splits(&self.s, &is_word_separator)
}
pub fn sentence_count(&self) -> usize {
- self.s
- .split(is_sentance_separator)
- .filter(is_not_empty)
- .count()
+ count_splits(&self.s, &is_sentance_separator)
}
pub fn emotion(&self) -> String {
- let text = self.s.replace("???", "?").replace("...", ".");
+ let text = self.s.replace("...", ".");
- let e_count = count_if(&text, &|elem| elem == '!');
- let q_count = count_if(&text, &|elem| elem == '?');
- let n_count = count_if(&text, &|elem| elem == '.');
+ let e_count = count_chars(&text, &|elem| elem == '!');
+ let q_count = count_chars(&text, &|elem| elem == '?');
+ let n_count = count_chars(&text, &|elem| elem == '.');
if e_count > q_count && e_count >= n_count {
String::from("😮")
} else if q_count > e_count && q_count >= n_count {
String::from("🤔")
} else {
String::from("😐")
}
}
}
// Helper Functions
-fn count_if(s: &str, fun: &Fn(char) -> bool) -> usize {
+fn count_chars(s: &str, fun: &Fn(char) -> bool) -> usize {
s.chars().fold(0, |counter, curr| if fun(curr) {
counter + 1
} else {
counter
})
}
+fn count_splits(s: &str, fun: &Fn(char) -> bool) -> usize {
+ s.split(fun).filter(is_not_empty).count()
+}
+
fn is_latin(c: char) -> bool {
match c {
'a'...'z' | 'A'...'Z' => true,
_ => false,
}
}
fn is_cyrillic(c: char) -> bool {
match c {
'а'...'я' | 'А'...'Я' => true,
_ => false,
}
}
fn is_letter(c: char) -> bool {
is_latin(c) || is_cyrillic(c)
}
fn is_sentance_separator(c: char) -> bool {
c == '.' || c == '!' || c == '?'
}
fn is_word_separator(c: char) -> bool {
!is_letter(c)
}
fn is_not_empty(s: &&str) -> bool {
!s.is_empty()
}
#[cfg(test)]
mod tests {
#[test]
fn test_basic() {
let t = ::TextInfo::new("abc. 123. абв.");
assert_eq!(14, t.char_count());
assert_eq!(6, t.alphabetic_count());
assert_eq!(3, t.cyrillic_letter_count());
assert_eq!(3, t.latin_letter_count());
assert_eq!(2, t.word_count());
assert_eq!(3, t.sentence_count());
assert_eq!("😐", t.emotion());
}
#[test]
fn test_empty_string() {
let empty_str = ::TextInfo::new("");
assert_eq!(0, empty_str.char_count());
assert_eq!(0, empty_str.alphabetic_count());
assert_eq!(0, empty_str.cyrillic_letter_count());
assert_eq!(0, empty_str.latin_letter_count());
assert_eq!(0, empty_str.sentence_count());
assert_eq!("😐", empty_str.emotion());
}
#[test]
fn test_multiple_punctoation() {
- let tmp = ::TextInfo::new("Who are you???");
+ let tmp = ::TextInfo::new("Who are you?");
- assert_eq!(14, tmp.char_count());
+ assert_eq!(12, tmp.char_count());
assert_eq!(1, tmp.sentence_count());
assert_eq!(0, tmp.cyrillic_letter_count());
assert_eq!("🤔", tmp.emotion())
}
#[test]
fn test_cyrillic() {
let cyr = ::TextInfo::new("Аз съм Иван!");
assert_eq!(12, cyr.char_count());
assert_eq!(9, cyr.cyrillic_letter_count());
assert_eq!("😮", cyr.emotion());
}
#[test]
fn test_count_sentences() {
let first = ::TextInfo::new("One, two, and three!");
let second = ::TextInfo::new("Huh? What's this??");
let third = ::TextInfo::new("Hmm... Hm, ... Hm, Hmm.");
let fourth = ::TextInfo::new("?...!");
let fifth = ::TextInfo::new(",?");
let sixth = ::TextInfo::new(".?");
assert_eq!(1, first.sentence_count());
assert_eq!(2, second.sentence_count());
assert_eq!(3, third.sentence_count());
assert_eq!(0, fourth.sentence_count());
assert_eq!(1, fifth.sentence_count());
assert_eq!(0, sixth.sentence_count());
}
#[test]
fn test_count_words() {
let first = ::TextInfo::new("One, two, and three!");
let second = ::TextInfo::new("Huh? What's this??");
let third = ::TextInfo::new("Hmm... Hm, Hm, Hmm.");
let fourth = ::TextInfo::new("?...!");
let fifth = ::TextInfo::new("");
let sixth = ::TextInfo::new("Баба,дядо");
let seventh = ::TextInfo::new("~*_1-баба-x-дядо-1_*~");
assert_eq!(4, first.word_count());
assert_eq!(4, second.word_count());
assert_eq!(4, third.word_count());
assert_eq!(0, fourth.word_count());
assert_eq!(0, fifth.word_count());
assert_eq!(2, sixth.word_count());
assert_eq!(3, seventh.word_count());
}
}
Иван качи решение на 21.10.2017 23:37 (преди почти 8 години)
#[derive(Debug)]
pub struct TextInfo {
s: String, // remove later pub
}
impl TextInfo {
pub fn new(s: &str) -> Self {
TextInfo { s: String::from(s) }
}
pub fn char_count(&self) -> usize {
self.s.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
count_chars(&self.s, &|elem| elem.is_alphabetic())
}
pub fn latin_letter_count(&self) -> usize {
count_chars(&self.s, &is_latin)
}
pub fn cyrillic_letter_count(&self) -> usize {
count_chars(&self.s, &is_cyrillic)
}
pub fn word_count(&self) -> usize {
count_splits(&self.s, &is_word_separator)
}
pub fn sentence_count(&self) -> usize {
count_splits(&self.s, &is_sentance_separator)
}
pub fn emotion(&self) -> String {
- let text = self.s.replace("...", ".");
+ let text = replace_punctuation(&self.s);
+ println!("Emotion: {:?} ", text);
+
let e_count = count_chars(&text, &|elem| elem == '!');
let q_count = count_chars(&text, &|elem| elem == '?');
let n_count = count_chars(&text, &|elem| elem == '.');
if e_count > q_count && e_count >= n_count {
String::from("😮")
} else if q_count > e_count && q_count >= n_count {
String::from("🤔")
} else {
String::from("😐")
}
}
}
// Helper Functions
fn count_chars(s: &str, fun: &Fn(char) -> bool) -> usize {
s.chars().fold(0, |counter, curr| if fun(curr) {
counter + 1
} else {
counter
})
}
Защо и тук не направиш един .filter(...).count()
, както по-долу?
fn count_splits(s: &str, fun: &Fn(char) -> bool) -> usize {
s.split(fun).filter(is_not_empty).count()
}
fn is_latin(c: char) -> bool {
match c {
'a'...'z' | 'A'...'Z' => true,
_ => false,
}
}
fn is_cyrillic(c: char) -> bool {
match c {
'а'...'я' | 'А'...'Я' => true,
_ => false,
}
}
fn is_letter(c: char) -> bool {
is_latin(c) || is_cyrillic(c)
}
fn is_sentance_separator(c: char) -> bool {
c == '.' || c == '!' || c == '?'
}
fn is_word_separator(c: char) -> bool {
!is_letter(c)
}
fn is_not_empty(s: &&str) -> bool {
!s.is_empty()
}
+fn replace_punctuation(s: &str) -> String {
+ let text = s.replace("!!", "!").replace("??", "?").replace("..", ".");
+ if text == s {
+ text
+ } else {
+ replace_punctuation(&text)
+ }
+}
+
#[cfg(test)]
mod tests {
#[test]
fn test_basic() {
let t = ::TextInfo::new("abc. 123. абв.");
assert_eq!(14, t.char_count());
assert_eq!(6, t.alphabetic_count());
assert_eq!(3, t.cyrillic_letter_count());
assert_eq!(3, t.latin_letter_count());
assert_eq!(2, t.word_count());
assert_eq!(3, t.sentence_count());
assert_eq!("😐", t.emotion());
}
#[test]
fn test_empty_string() {
let empty_str = ::TextInfo::new("");
assert_eq!(0, empty_str.char_count());
assert_eq!(0, empty_str.alphabetic_count());
assert_eq!(0, empty_str.cyrillic_letter_count());
assert_eq!(0, empty_str.latin_letter_count());
assert_eq!(0, empty_str.sentence_count());
assert_eq!("😐", empty_str.emotion());
}
#[test]
fn test_multiple_punctoation() {
let tmp = ::TextInfo::new("Who are you?");
assert_eq!(12, tmp.char_count());
assert_eq!(1, tmp.sentence_count());
assert_eq!(0, tmp.cyrillic_letter_count());
assert_eq!("🤔", tmp.emotion())
}
#[test]
fn test_cyrillic() {
let cyr = ::TextInfo::new("Аз съм Иван!");
assert_eq!(12, cyr.char_count());
assert_eq!(9, cyr.cyrillic_letter_count());
assert_eq!("😮", cyr.emotion());
}
#[test]
fn test_count_sentences() {
let first = ::TextInfo::new("One, two, and three!");
let second = ::TextInfo::new("Huh? What's this??");
let third = ::TextInfo::new("Hmm... Hm, ... Hm, Hmm.");
let fourth = ::TextInfo::new("?...!");
let fifth = ::TextInfo::new(",?");
let sixth = ::TextInfo::new(".?");
assert_eq!(1, first.sentence_count());
assert_eq!(2, second.sentence_count());
assert_eq!(3, third.sentence_count());
assert_eq!(0, fourth.sentence_count());
assert_eq!(1, fifth.sentence_count());
assert_eq!(0, sixth.sentence_count());
}
#[test]
fn test_count_words() {
let first = ::TextInfo::new("One, two, and three!");
let second = ::TextInfo::new("Huh? What's this??");
let third = ::TextInfo::new("Hmm... Hm, Hm, Hmm.");
let fourth = ::TextInfo::new("?...!");
let fifth = ::TextInfo::new("");
let sixth = ::TextInfo::new("Баба,дядо");
let seventh = ::TextInfo::new("~*_1-баба-x-дядо-1_*~");
+ let eighth = ::TextInfo::new("~*! !( ");
assert_eq!(4, first.word_count());
assert_eq!(4, second.word_count());
assert_eq!(4, third.word_count());
assert_eq!(0, fourth.word_count());
assert_eq!(0, fifth.word_count());
assert_eq!(2, sixth.word_count());
assert_eq!(3, seventh.word_count());
+ assert_eq!(0, eighth.word_count());
}
-}
+ #[test]
+ fn test_emotion() {
+ let first = ::TextInfo::new("Compiler error!! Oh, no... What just happened?");
+
+ assert_eq!("😐", first.emotion());
+ }
+}
Иван качи решение на 22.10.2017 17:25 (преди почти 8 години)
#[derive(Debug)]
pub struct TextInfo {
s: String, // remove later pub
}
impl TextInfo {
pub fn new(s: &str) -> Self {
TextInfo { s: String::from(s) }
}
pub fn char_count(&self) -> usize {
self.s.chars().count()
}
pub fn alphabetic_count(&self) -> usize {
count_chars(&self.s, &|elem| elem.is_alphabetic())
}
pub fn latin_letter_count(&self) -> usize {
count_chars(&self.s, &is_latin)
}
pub fn cyrillic_letter_count(&self) -> usize {
count_chars(&self.s, &is_cyrillic)
}
pub fn word_count(&self) -> usize {
count_splits(&self.s, &is_word_separator)
}
pub fn sentence_count(&self) -> usize {
- count_splits(&self.s, &is_sentance_separator)
+ let e_count = count_chars(&self.s, &|elem| elem == '!');
+ let q_count = count_chars(&self.s, &|elem| elem == '?');
+ let n_count = count_chars(&self.s, &|elem| elem == '.');
+
+ if e_count + q_count + n_count == 0 {
+ 0
+ } else {
+ count_splits(&self.s, &is_sentance_separator)
+ }
}
pub fn emotion(&self) -> String {
let text = replace_punctuation(&self.s);
println!("Emotion: {:?} ", text);
let e_count = count_chars(&text, &|elem| elem == '!');
let q_count = count_chars(&text, &|elem| elem == '?');
let n_count = count_chars(&text, &|elem| elem == '.');
if e_count > q_count && e_count >= n_count {
String::from("😮")
} else if q_count > e_count && q_count >= n_count {
String::from("🤔")
} else {
String::from("😐")
}
}
}
// Helper Functions
fn count_chars(s: &str, fun: &Fn(char) -> bool) -> usize {
s.chars().fold(0, |counter, curr| if fun(curr) {
counter + 1
} else {
counter
})
}
https://github.com/rust-lang/rust/issues/33038 Не съм сигурен дали още е релевантно (will research that later), но "The filter-count version doesn't seem to vectorize, while the fold version does." По-скоро въпросът е защо вместо filter(..).count() не използвам fold на другото място.
Oт друга страна, срещнах някои проблеми при просто извикване на .filter(..).count() над &str и оставих този проблем за по-късно разглеждане.
Разбирам, микрооптимизации :). Смело твърдя, че в случая няма да има осезаема разлика в performance-а :). Сподели като напишеш няколко benchmark-а, разбира се.
Втората причина е по съществена за въпроса.
fn count_splits(s: &str, fun: &Fn(char) -> bool) -> usize {
s.split(fun).filter(is_not_empty).count()
}
fn is_latin(c: char) -> bool {
match c {
'a'...'z' | 'A'...'Z' => true,
_ => false,
}
}
fn is_cyrillic(c: char) -> bool {
match c {
'а'...'я' | 'А'...'Я' => true,
_ => false,
}
}
fn is_letter(c: char) -> bool {
is_latin(c) || is_cyrillic(c)
}
fn is_sentance_separator(c: char) -> bool {
c == '.' || c == '!' || c == '?'
}
fn is_word_separator(c: char) -> bool {
!is_letter(c)
}
fn is_not_empty(s: &&str) -> bool {
!s.is_empty()
}
fn replace_punctuation(s: &str) -> String {
let text = s.replace("!!", "!").replace("??", "?").replace("..", ".");
if text == s {
text
} else {
replace_punctuation(&text)
}
-}
-
+}
-#[cfg(test)]
-mod tests {
- #[test]
- fn test_basic() {
- let t = ::TextInfo::new("abc. 123. абв.");
-
- assert_eq!(14, t.char_count());
- assert_eq!(6, t.alphabetic_count());
- assert_eq!(3, t.cyrillic_letter_count());
- assert_eq!(3, t.latin_letter_count());
- assert_eq!(2, t.word_count());
- assert_eq!(3, t.sentence_count());
- assert_eq!("😐", t.emotion());
- }
-
- #[test]
- fn test_empty_string() {
- let empty_str = ::TextInfo::new("");
-
- assert_eq!(0, empty_str.char_count());
- assert_eq!(0, empty_str.alphabetic_count());
- assert_eq!(0, empty_str.cyrillic_letter_count());
- assert_eq!(0, empty_str.latin_letter_count());
- assert_eq!(0, empty_str.sentence_count());
- assert_eq!("😐", empty_str.emotion());
- }
-
- #[test]
- fn test_multiple_punctoation() {
- let tmp = ::TextInfo::new("Who are you?");
-
- assert_eq!(12, tmp.char_count());
- assert_eq!(1, tmp.sentence_count());
- assert_eq!(0, tmp.cyrillic_letter_count());
- assert_eq!("🤔", tmp.emotion())
- }
-
- #[test]
- fn test_cyrillic() {
- let cyr = ::TextInfo::new("Аз съм Иван!");
-
- assert_eq!(12, cyr.char_count());
- assert_eq!(9, cyr.cyrillic_letter_count());
- assert_eq!("😮", cyr.emotion());
- }
-
- #[test]
- fn test_count_sentences() {
- let first = ::TextInfo::new("One, two, and three!");
- let second = ::TextInfo::new("Huh? What's this??");
- let third = ::TextInfo::new("Hmm... Hm, ... Hm, Hmm.");
- let fourth = ::TextInfo::new("?...!");
- let fifth = ::TextInfo::new(",?");
- let sixth = ::TextInfo::new(".?");
-
- assert_eq!(1, first.sentence_count());
- assert_eq!(2, second.sentence_count());
- assert_eq!(3, third.sentence_count());
- assert_eq!(0, fourth.sentence_count());
- assert_eq!(1, fifth.sentence_count());
- assert_eq!(0, sixth.sentence_count());
- }
-
- #[test]
- fn test_count_words() {
- let first = ::TextInfo::new("One, two, and three!");
- let second = ::TextInfo::new("Huh? What's this??");
- let third = ::TextInfo::new("Hmm... Hm, Hm, Hmm.");
- let fourth = ::TextInfo::new("?...!");
- let fifth = ::TextInfo::new("");
- let sixth = ::TextInfo::new("Баба,дядо");
- let seventh = ::TextInfo::new("~*_1-баба-x-дядо-1_*~");
- let eighth = ::TextInfo::new("~*! !( ");
-
- assert_eq!(4, first.word_count());
- assert_eq!(4, second.word_count());
- assert_eq!(4, third.word_count());
- assert_eq!(0, fourth.word_count());
- assert_eq!(0, fifth.word_count());
- assert_eq!(2, sixth.word_count());
- assert_eq!(3, seventh.word_count());
- assert_eq!(0, eighth.word_count());
- }
-
- #[test]
- fn test_emotion() {
- let first = ::TextInfo::new("Compiler error!! Oh, no... What just happened?");
-
- assert_eq!("😐", first.emotion());
- }
-}
Защо и тук не направиш един
.filter(...).count()
, както по-долу?https://github.com/rust-lang/rust/issues/33038 Не съм сигурен дали още е релевантно (will research that later), но "The filter-count version doesn't seem to vectorize, while the fold version does." По-скоро въпросът е защо вместо filter(..).count() не използвам fold на другото място.
Oт друга страна, срещнах някои проблеми при просто извикване на .filter(..).count() над &str и оставих този проблем за по-късно разглеждане.
Разбирам, микрооптимизации :). Смело твърдя, че в случая няма да има осезаема разлика в performance-а :). Сподели като напишеш няколко benchmark-а, разбира се.
Втората причина е по съществена за въпроса.