João Paulo Dubas 410a509faa
Some checks reported errors
continuous-integration/drone/push Build encountered an error
chore: apply credo suggestions (#3)
Mainly add module docs and fix some aliases.
2022-04-22 00:49:00 +00:00

33 lines
728 B
Elixir

defmodule Wabanex.IMC do
@moduledoc """
Body mass index calculation
"""
def calculate(%{"filename" => filename}) do
filename
|> File.read()
|> handle_file()
end
defp handle_file({:ok, content}) do
data =
content |> String.trim() |> String.split("\n") |> Enum.map(&parse_line/1) |> Enum.into(%{})
{:ok, data}
end
defp handle_file({:error, _reason}) do
{:error, "Error while opening the file"}
end
defp parse_line(line) do
line
|> String.split(",")
|> List.update_at(1, &String.to_float/1)
|> List.update_at(2, &String.to_float/1)
|> calculate_imc()
end
defp calculate_imc([name, height, weight]), do: {name, weight / :math.pow(height / 100, 2)}
end