Reputation: 363
I have set of vowels:
val vowels = immutable.Set("A", "E", "I", "O", "U", "Y", "a", "e", "o", "u", "y")
And I have a set of words. And I need to check if first letter of word from set belongs to vowels.
Upvotes: 1
Views: 597
Reputation: 181
Assuming word is a string:
vowels(word.head)
will return a boolean. And depending on use case you could do something like this:
words.map(vowels(_.head))
words.filter(vowels(_.head))
Upvotes: 2
Reputation: 20285
To check a single word:
scala> vowels.contains("foo".head.toString)
res2: Boolean = false
scala> vowels.contains("oops".head.toString)
res3: Boolean = true
To check a List
of words:
scala> val words = List("foo", "bar", "ate", "elf", "baz")
words: List[String] = List(foo, bar, ate, elf, baz)
scala> words.map(w => vowels.contains(w.head.toString))
res8: List[Boolean] = List(false, false, true, true, false)
You can also define vowels
as a Char
then use toLower
in your test:
scala> val vowels = Set('a', 'e', 'i', 'o', 'u')
vowels: scala.collection.immutable.Set[Char] = Set(e, u, a, i, o)
scala> vowels.contains("Oops".head.toLower)
res18: Boolean = true
Upvotes: 3