I have recently tried Codewars to practice Elixir and I have to admit I am enjoying it quite a lot.
One of the exercises tells me to count vowels. I did my solution and after that I compared it with others. This specific solution baffles me as I don’t quite understand it:
defmodule VowelCount do
@vowels ~w[a i u e o]
def get_count(str) do
str
|> String.graphemes
|> Enum.count(&(&1 in @vowels))
end
end
Question
My question here lies in the line @vowels ~w[a i u e o]. I know that @vowels is a constant in the module, but the ~w syntax is complete new to me. What does it mean?
In addition to what others have said, ~w is designed to mimic the behavior of %w from ruby and I believe perl as well. My understanding is that the w stands for word, which is why it splits on whitespace to produce all “words”.