Sha256: 83b709c4af712cc1dc4753c27732e75df9d6a6487def1116bef3206485e38771

Contents?: true

Size: 570 Bytes

Versions: 69

Compression:

Stored size: 570 Bytes

Contents

defmodule Words do
  @ascii_punctuation ~r/!|"|\#|\$|%|&|'|\(|\)|\*|\+|,|\.|\/|:|;|<|=|>|\?|@|\[|\\|]|\^|_|`|\{|\||}|~/

  def count(sentence) do
    sentence
    |> String.downcase()
    |> remove_punctuation
    |> to_words
    |> summarize
  end

  defp remove_punctuation(string), do: String.replace(string, @ascii_punctuation, " ")

  defp to_words(sentence), do: List.flatten(String.split(sentence))

  defp summarize(words) do
    Enum.reduce(words, %{}, &add_count/2)
  end

  defp add_count(word, counts) do
    Map.update(counts, word, 1, &(&1 + 1))
  end
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.119 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.118 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.117 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.116 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.115 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.114 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.113 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.111 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.110 tracks/elixir/exercises/word-count/example.exs