sum_list = [5, "four", "two", 10, "one", 28, 6, "five"]
defmodule SumModule do
@words_with_index Enum.with_index(["one", "two", "three", "four", "five"], 1)
@words_to_numbers Enum.into(%{}, @words_with_index)
def sum_list(list) do
list
|> Enum.map(&to_integer/1)
|> Enum.sum()
end
defp to_integer(number) when is_integer(number), do: number
defp to_integer(other), do: Map.get(@words_to_numbers, other, 0)
end
IO.puts SumModule.sum_list(sum_list)