tracks/elixir/exercises/hexadecimal/example.exs in trackler-2.2.1.109 vs tracks/elixir/exercises/hexadecimal/example.exs in trackler-2.2.1.110
- old
+ new
@@ -1,10 +1,10 @@
defmodule Hexadecimal do
@base 16
@hex_table String.split("0123456789abcdef", "", trim: true)
- |> Enum.with_index
- |> Enum.into(%{})
+ |> Enum.with_index()
+ |> Enum.into(%{})
@doc """
Accept a string representing a hexadecimal value and return the
corresponding decimal value.
It returns the integer 0 if the hexadecimal is invalid.
@@ -24,22 +24,23 @@
def to_decimal(hex) do
if invalid?(hex) do
0
else
hex
- |> String.downcase
- |> String.reverse
- |> String.split("", trim: true)
- |> hex_to_int(0, 0)
+ |> String.downcase()
+ |> String.reverse()
+ |> String.split("", trim: true)
+ |> hex_to_int(0, 0)
end
end
defp invalid?(hex) do
String.match?(hex, ~r/[^0-9a-fA-F]/)
end
defp hex_to_int([], acc, _), do: round(acc)
- defp hex_to_int([char|hex], acc, power) do
+
+ defp hex_to_int([char | hex], acc, power) do
acc = acc + Map.get(@hex_table, char) * :math.pow(@base, power)
hex_to_int(hex, acc, power + 1)
end
end