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.180 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/word-count/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/word-count/example.exs