summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkballou <kballou@devnulllabs.io>2016-03-19 01:34:35 -0600
committerkballou <kballou@devnulllabs.io>2016-03-19 02:04:04 -0600
commitdd06483b444fb6b21ba8b75c2841f0acf841c4f3 (patch)
tree122475bf122adde6e10363d0102005b02d6908fe
parente48f0a9ea5e9cff2b655aebefe642489f619d316 (diff)
downloadfunc-w-elixir-dd06483b444fb6b21ba8b75c2841f0acf841c4f3.tar.gz
func-w-elixir-dd06483b444fb6b21ba8b75c2841f0acf841c4f3.tar.xz
Add wc_2, small improvement step
-rw-r--r--src/code/2/wc_2.exs29
-rw-r--r--src/func-w-elixir.tex5
2 files changed, 34 insertions, 0 deletions
diff --git a/src/code/2/wc_2.exs b/src/code/2/wc_2.exs
new file mode 100644
index 0000000..f7e97e9
--- /dev/null
+++ b/src/code/2/wc_2.exs
@@ -0,0 +1,29 @@
+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
+ |> Stream.map(&String.downcase/1)
+ |> Enum.to_list
+ |> reduce_words
+ end
+
+end
+
+WordCount.count_words("pg51353.txt") |> IO.inspect(limit: 20)
diff --git a/src/func-w-elixir.tex b/src/func-w-elixir.tex
index 8fcb41a..6f0ab9f 100644
--- a/src/func-w-elixir.tex
+++ b/src/func-w-elixir.tex
@@ -417,6 +417,11 @@ F_1 &= 1
\lstinputlisting{code/2/wc.out}
\end{frame}
+\begin{frame}[fragile]
+\frametitle{Improve Word Counting}
+\lstinputlisting[numbers=left,firstline=19,lastline=25]{code/2/wc_2.exs}
+\end{frame}
+
\subsubsection{Parallel Map}
\begin{frame}
\frametitle{Parallel Map}