Responses for exercises in Exercism.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

25 lines
549 B

defmodule WordCount do
@doc """
Count the number of words in the sentence.
Words are compared case-insensitively.
"""
@spec count(String.t()) :: map
def count(sentence) do
sentence
|> String.split(~r/[ _ ]/)
|> Enum.reduce(%{}, fn (word, acc) ->
lower_word = word
|> String.downcase
|> String.replace(~r/[\$!&^%,:@]/, "")
if String.length(lower_word) > 0 do
count = Map.get(acc, lower_word, 0)
Map.put(acc, lower_word, count + 1)
else
acc
end
end)
end
end