elixir-in-action/chapter3/enum_streams_practice.ex

38 lines
664 B
Elixir
Raw Permalink Normal View History

2020-06-20 13:57:33 -05:00
defmodule FileHelper do
defp filtered_lines!(path) do
File.stream!(path)
|> Stream.map(&String.replace(&1, "\n", ""))
end
def line_lengths!(path) do
path
|> filtered_lines!()
|> Enum.map(&String.length/1)
end
def longest_line_length(path) do
path
|> filtered_lines!()
|> Stream.map(&String.length/1)
|> Enum.max()
end
def longest_line(path) do
path
|> filtered_lines!()
|> Enum.max_by(&String.length/1)
end
def words_per_line(path) do
path
|> filtered_lines!()
|> Enum.map(&word_count/1)
end
def word_count(string) do
string
|> String.split()
|> length()
end
end