exercism/elixir/raindrops/lib/raindrops.ex

31 lines
786 B
Elixir
Raw Permalink Normal View History

2024-06-27 21:22:40 +00:00
defmodule Raindrops do
@doc """
Returns a string based on raindrop factors.
- If the number contains 3 as a prime factor, output 'Pling'.
- If the number contains 5 as a prime factor, output 'Plang'.
- If the number contains 7 as a prime factor, output 'Plong'.
- If the number does not contain 3, 5, or 7 as a prime factor,
just pass the number's digits straight through.
"""
@results [
{3, "Pling"},
{5, "Plang"},
{7, "Plong"}
]
@spec convert(pos_integer) :: String.t()
def convert(number) do
@results
|> Enum.map_join(&mapper(number, &1))
|> case do
"" -> Integer.to_string(number)
result -> result
end
end
defp mapper(number, {factor, value}) do
if rem(number, factor) == 0, do: value, else: ""
end
end