exercism/elixir/word-count/word_count.exs

13 lines
350 B
Elixir

defmodule Words do
@doc """
Count the number of words in the sentence.
Words are compared case-insensitively.
"""
@spec count(String.t()) :: map
def count(sentence) do
String.split(Regex.replace(~r/[^[:alnum:]-]/u, String.downcase(sentence), " "))
|> Enum.reduce(%{}, fn w, acc -> Map.update(acc, w, 1, &(&1 + 1)) end)
end
end