Skip to content

Instantly share code, notes, and snippets.

@jbowles
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbowles/6110a0a94f19e61d2699 to your computer and use it in GitHub Desktop.
Save jbowles/6110a0a94f19e61d2699 to your computer and use it in GitHub Desktop.
defmodule WordCounter do
def count(sent) do
sent |> normalize |> find_words |> count_unique |> Enum.sort()
end
def find_words(sent) do
Regex.scan(~r/\w+/, sent)
end
def normalize(string) do
String.downcase(string)
end
def count_unique(words), do: count_unique(HashDict.new, words)
defp count_unique(hash, [head|tail]) do
#hash = HashDict.update hash, head, 1, fn val -> val + 1 end
hash = HashDict.update hash, head, 1, &(&1 + 1)
count_unique hash, tail
end
defp count_unique(hash, []), do: hash
end
#Usage
#s = "Check out this this sentece sentence and and and and it has some words for counting and passing data from one one one function to another... yay yay, yay."
#WordCounter.count(s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment