Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Episode 5 source code (challenge 2)
filename =
IO.gets("File to count the words from (h for help):\n")
|> String.trim
if filename == "h" do
IO.puts """
Usage: [filename] -[flags]
-l displays line count
-c displays character count
-w displays word count (default)
Multiple flags may be used. Example usage to display line and character count:
somefile.txt -lc
parts = String.split(filename, "-")
filename = List.first(parts) |> String.trim
flags = case, 1) do
# set only "w" flag if none were set
nil -> ["w"]
chars -> String.split(chars, "") |> Enum.filter(fn x -> x != "" end)
body =! filename
lines = String.split(body, ~r{(\r\n|\n|\r)})
words =
String.split(body, ~r{(\\n|[^\w'])+})
|> Enum.filter(fn x -> x != "" end)
chars = String.split(body, "") |> Enum.filter(fn x -> x != "" end)
Enum.each(flags, fn flag ->
case flag do
"l" -> IO.puts "Lines: #{Enum.count(lines)}"
"w" -> IO.puts "Words: #{Enum.count(words)}"
"c" -> IO.puts "Chars: #{Enum.count(chars)}"
_ -> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.