dart-exercism/word-count/lib/word_count.dart

17 lines
439 B
Dart
Raw Normal View History

2024-10-14 11:37:17 +02:00
class WordCount {
Map<String, int> countWords(String input) {
var exp = new RegExp(r"(\w+('\w+)?)");
return exp
.allMatches(input)
.map((e) => e.group(0)!.toLowerCase())
.fold(new Map<String, int>(), (Map<String, int> counts, String word) {
if (counts.containsKey(word)) {
counts[word] = counts[word]! + 1;
} else {
counts[word] = 1;
}
return counts;
});
}
}