Great article, thanks! I'm not an expert Alchemist by any means, but whenever I see a `cond`, I try to see if it can be refactored out using pattern matching (after I was picked up on the same thing during an interview!) defp formatted_hour("AM", "12"), do: "00" defp formatted_hour("AM", hours), do: hours defp formatted_hour("PM", "12"), do: "12" defp formatted_hour("PM", hours), do: 12 + String.to_integer(hours)
I agree though one thing I would caution is it is good practice to always return the same type from a function even though it is an untyped language. The whole thing can be boiled down even more if pattern matching is used in parsing the initial string. def convert( > ":" > ":" > >) do "#{formatted_hour(h,period)}:#{m}:#{s}" end defp formatted_hour(hours,"PM"), do: "#{String.to_integer(hours) + 12}" defp formatted_ho…
def convert("12:" > ":" > "AM") do
"00:#{m}:#{s}"
end
def convert(> ":" > ":" > "PM") do
h = 12 + String.to_integer(h)
"#{h}:#{m}:#{s}"
end
def convert(> ":" > ":" > "AM") do
"#{h}:#{m}:#{s}"
end
Whether that's an improvement or not is another matter :)