Skip to content

Instantly share code, notes, and snippets.

@jbowles jbowles/word_counter.ex
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.