Skip to content

Instantly share code, notes, and snippets.

@lagoushque
Created January 29, 2017 00:48
Show Gist options
  • Save lagoushque/d514e9dd134978e4bdcd601bd0e1cf38 to your computer and use it in GitHub Desktop.
Save lagoushque/d514e9dd134978e4bdcd601bd0e1cf38 to your computer and use it in GitHub Desktop.
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(String.downcase(sentence), ~r/[^[:alnum:]\-]+/u, trim: true)
|>Enum.reduce(%{}, fn(tag, acc) -> Map.update(acc, tag, 1, &(&1+1))end)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment