@pragdave
I think all the annoymous functions in the exercise solutions miss the “&” because we will get error: capture argument &1 must be used within the capture operator &.
The solution will have error:
Function read/1 has no local return.The function call will not succeed.
IO.stream(_file :: pid())
will never return since the 1st arguments differ
from the success typing arguments:
(:line | pos_integer())
def read(filename) do
file = File.open!(filename)
headers = read_headers(IO.read(file, :line))
Enum.map(IO.stream(file), &create_one_row(headers, &1))
end
Changing to this will be good. And it should be ~r for the regular expression.
defmodule SimpleCSV do
def read(filename) do
file = filename |> File.open!()
headers = file |> IO.read(:line) |> read_headers()
result =
file
|> IO.stream(:line)
|> Enum.map(&create_one_row(headers, &1))
File.close(file)
result
end
defp read_headers(hdr_line) do
from_csv_and_map(hdr_line, &String.to_atom(&1))
end
defp create_one_row(headers, row_csv) do
row = from_csv_and_map(row_csv, &maybe_convert_numbers(&1))
Enum.zip(headers, row)
end
defp from_csv_and_map(row_csv, mapper) do
row_csv
|> String.trim()
|> String.split(~r{,\s*})
|> Enum.map(mapper)
end
defp maybe_convert_numbers(value) do
cond do
Regex.match?(~r{^\d+$}, value) -> String.to_integer(value)
Regex.match?(~r{^\d+\.\d+$}, value) -> String.to_float(value)
<<?:::utf8, name::binary>> = value -> String.to_atom(name)
true -> value
end
end
end