Skip to content

Instantly share code, notes, and snippets.

@knewter
Last active August 29, 2015 14:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save knewter/be745ae2094dad324baa to your computer and use it in GitHub Desktop.
Save knewter/be745ae2094dad324baa to your computer and use it in GitHub Desktop.
defmodule WordStats do
def letter_frequency words do
words
|> Enum.reduce(%{}, fn(word, map) ->
word
|> String.graphemes
|> Enum.reduce(map, fn(letter, map) ->
Map.update(map, letter, 1, &(&1+1))
end)
end)
end
end
defmodule TaskPlaygroundTest do
use ExUnit.Case
test "the truth" do
assert 1 + 1 == 2
#words = File.read!("/usr/share/dict/words") |> String.split("\n")
words = [
"cat",
"bag"
]
assert %{"c" => 1, "a" => 2, "t" => 1, "b" => 1, "g" => 1} == WordStats.letter_frequency(words)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment