I'm studying functional programming with Elixir and came across the following exercise:
"You are given a two-digit integer n. Return the sum of its digits."
The solution I came up looks a bit "hairy".
I wonder if someone could give some advice into Elixir std lib functions/module and provide a better solution.
I know I could just go with n%10 + Math.floor(n/10)
(js) but Id' like to know if a solution using Elixir functions would be more or less what I came up with:
def addTwoDigits(n) do
n |> Integer.to_string
|> String.split("") # Number 44 would give a list ["",4,4,""]
|> Enum.filter(&(&1 != ""))
|> Enum.map(&(String.to_integer(&1)))
|> Enum.reduce(&(&1+&2))
end