Skip to content

Instantly share code, notes, and snippets.

@vKxni
Created December 25, 2022 20:03
Show Gist options
  • Save vKxni/c34c320923549084b523849da5387715 to your computer and use it in GitHub Desktop.
Save vKxni/c34c320923549084b523849da5387715 to your computer and use it in GitHub Desktop.
defmodule Word do
def count(string) do
words = String.split(string, " ")
words = words |> Enum.map(&String.trim/1) |> Enum.map(&String.downcase/1)
words = Enum.filter(words, &(String.length(&1) > 1))
word_counts =
Enum.reduce(words, %{}, fn word, counts ->
Map.update(counts, word, 1, &(&1 + 1))
end)
word_counts
end
end
# Word.count("This is a test string") => %{"is" => 1, "string" => 1, "test" => 1, "this" => 1}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment