Skip to content

Instantly share code, notes, and snippets.

@tomgilder
Created July 2, 2020 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomgilder/7121cb5588b7e0fcd9511568c0e0fe32 to your computer and use it in GitHub Desktop.
Save tomgilder/7121cb5588b7e0fcd9511568c0e0fe32 to your computer and use it in GitHub Desktop.
enum counting
void main() {
final words = ['a', 'dog', 'apples', 'supercalifragilisticexpialidocious'];
final lengths = words.map((word) => getWordLength(word));
print("Long words: ${lengths.where((length) => length == WordLength.long).length}");
print("Medium words: ${lengths.where((length) => length == WordLength.medium).length}");
print("Short words: ${lengths.where((length) => length == WordLength.short).length}");
}
WordLength getWordLength(String word) {
if (word.length > 10) {
return WordLength.long;
}
if (word.length > 5) {
return WordLength.medium;
}
return WordLength.short;
}
enum WordLength {
short,
medium,
long
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment