Created
December 25, 2022 20:03
-
-
Save vKxni/c34c320923549084b523849da5387715 to your computer and use it in GitHub Desktop.
This file contains 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 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