Skip to content

Instantly share code, notes, and snippets.

@bind-disney
Created August 12, 2018 15:33
Show Gist options
  • Save bind-disney/3b19351f9a20950def201815ca646b22 to your computer and use it in GitHub Desktop.
Save bind-disney/3b19351f9a20950def201815ca646b22 to your computer and use it in GitHub Desktop.
"""
You should add Flow as a dependency to your Mix project configuration:
mix.exs:
defp deps do
[
...,
{:flow, "~> 0.14.2"},
...
]
end
## Examples:
iex> WordCounter.flow_wordcount("lib/word_counter.ex")
%{
"Flow.reduce(" => 1,
"&" => 3,
"word," => 6,
"Flow.flat_map(&String.split/1)" => 1,
"%{}" => 1,
"Flow.partition()" => 1,
"Map.update(map," => 3,
"Enum.flat_map(&String.split/1)" => 1,
"&1" => 3,
"Flow.from_enumerable()" => 1,
"lazy_wordcount(filepath)" => 1,
"def" => 3,
"end" => 4,
"eager_wordcount(filepath)" => 1,
"File.read!(filepath)" => 1,
"File.stream!(filepath)" => 2,
"->" => 4,
"Enum.reduce(%{}," => 2,
"end," => 1,
"WordCounter" => 1,
"Enum.into(%{})" => 1,
"defmodule" => 1,
"do" => 4,
"flow_wordcount(filepath)" => 1,
"fn" => 4,
"1," => 3,
"+" => 3,
"end)" => 3,
"Stream.flat_map(&String.split/1)" => 1,
"|>" => 10,
"1)" => 3,
"String.split(\"\\n\")" => 1,
"map" => 3
}
"""
defmodule WordCounter do
def eager_wordcount(filepath) do
File.read!(filepath)
|> String.split("\n")
|> Enum.flat_map(&String.split/1)
|> Enum.reduce(%{}, fn word, map -> Map.update(map, word, 1, & &1 + 1) end)
end
def lazy_wordcount(filepath) do
File.stream!(filepath)
|> Stream.flat_map(&String.split/1)
|> Enum.reduce(%{}, fn word, map -> Map.update(map, word, 1, & &1 + 1) end)
end
def flow_wordcount(filepath) do
File.stream!(filepath)
|> Flow.from_enumerable()
|> Flow.flat_map(&String.split/1)
|> Flow.partition()
|> Flow.reduce(
fn -> %{} end,
fn word, map -> Map.update(map, word, 1, & &1 + 1) end)
|> Enum.into(%{})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment