Last active
August 29, 2015 14:05
-
-
Save jbowles/6110a0a94f19e61d2699 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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