diff options
author | kballou <kballou@devnulllabs.io> | 2016-03-04 15:56:47 -0700 |
---|---|---|
committer | kballou <kballou@devnulllabs.io> | 2016-03-07 15:31:22 -0700 |
commit | 3c20dbbdf4ca14fab918576a1479c7aa90196e6e (patch) | |
tree | 83be80fb18b20c3440d3e87f8739c6d1780b4223 /src/code/2/wc.exs | |
parent | bc484b7444ff68494b2da7b16ceefb0b1e4a396c (diff) | |
download | func-w-elixir-3c20dbbdf4ca14fab918576a1479c7aa90196e6e.tar.gz func-w-elixir-3c20dbbdf4ca14fab918576a1479c7aa90196e6e.tar.xz |
Add code examples to presentation
Diffstat (limited to 'src/code/2/wc.exs')
-rw-r--r-- | src/code/2/wc.exs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/code/2/wc.exs b/src/code/2/wc.exs new file mode 100644 index 0000000..2c7c284 --- /dev/null +++ b/src/code/2/wc.exs @@ -0,0 +1,28 @@ +defmodule WordCount do + + defp stream_file(filename) do + File.stream!(filename) + end + + defp tokenize_words(line) do + line |> Stream.flat_map(&String.split/1) + end + + defp reduce_words(words) when is_list(words) do + Enum.reduce(words, %{}, &update_count/2) + end + + defp update_count(word, acc) do + Map.update(acc, word, 1, &(&1 + 1)) + end + + def count_words(filename) do + stream_file(filename) + |> tokenize_words + |> Enum.to_list + |> reduce_words + end + +end + +WordCount.count_words("pg51353.txt") |> IO.inspect(limit: 20) |