# davearonson's solution

## to Raindrops in the Elixir Track

Published at Jul 13 2018 · 5 comments
Instructions
Test suite
Solution

#### Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

Convert a number to a string, the contents of which depend on the number's factors.

• If the number has 3 as a factor, output 'Pling'.
• If the number has 5 as a factor, output 'Plang'.
• If the number has 7 as a factor, output 'Plong'.
• If the number does not have 3, 5, or 7 as a factor, just pass the number's digits straight through.

## Examples

• 28's factors are 1, 2, 4, 7, 14, 28.
• In raindrop-speak, this would be a simple "Plong".
• 30's factors are 1, 2, 3, 5, 6, 10, 15, 30.
• In raindrop-speak, this would be a "PlingPlang".
• 34 has four factors: 1, 2, 17, and 34.
• In raindrop-speak, this would be "34".

## Running tests

Execute the tests with:

\$ elixir raindrops_test.exs

### Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

For more detailed information about the Elixir track, please see the help page.

## Source

A variation on a famous interview question intended to weed out potential candidates. http://jumpstartlab.com

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### raindrops_test.exs

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule RaindropsTest do
use ExUnit.Case

# @tag :pending
test "1" do
assert Raindrops.convert(1) == "1"
end

@tag :pending
test "3" do
assert Raindrops.convert(3) == "Pling"
end

@tag :pending
test "5" do
assert Raindrops.convert(5) == "Plang"
end

@tag :pending
test "7" do
assert Raindrops.convert(7) == "Plong"
end

@tag :pending
test "6" do
assert Raindrops.convert(6) == "Pling"
end

@tag :pending
test "9" do
assert Raindrops.convert(9) == "Pling"
end

@tag :pending
test "10" do
assert Raindrops.convert(10) == "Plang"
end

@tag :pending
test "14" do
assert Raindrops.convert(14) == "Plong"
end

@tag :pending
test "15" do
assert Raindrops.convert(15) == "PlingPlang"
end

@tag :pending
test "21" do
assert Raindrops.convert(21) == "PlingPlong"
end

@tag :pending
test "25" do
assert Raindrops.convert(25) == "Plang"
end

@tag :pending
test "35" do
assert Raindrops.convert(35) == "PlangPlong"
end

@tag :pending
test "49" do
assert Raindrops.convert(49) == "Plong"
end

@tag :pending
test "52" do
assert Raindrops.convert(52) == "52"
end

@tag :pending
test "105" do
assert Raindrops.convert(105) == "PlingPlangPlong"
end

@tag :pending
test "12121" do
assert Raindrops.convert(12121) == "12121"
end
end
defmodule Raindrops do

@noises %{ 3 => "Pling", 5 => "Plang", 7 => "Plong" }

@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.
"""
@spec convert(pos_integer) :: String.t
def convert(number) do
make_noises(number) || Integer.to_string(number)
end

defp make_noises(number) do
@noises |> Map.keys
|> Enum.filter(&(rem(number, &1) == 0))
|> Enum.map_join(&(@noises[&1]))
|> presence
end

defp presence("" ), do: nil
defp presence(str), do: str

end

And now even more elixirish, transforming Map.keys(@noises) to @noises |> Map.keys....

Woow, this is amazing!

This is brilliant. Thanks for providing this solution!

Thanks! I didn't really consider it all that brilliant -- the main thing that lets it work so simply is the presence function, which I simply swiped from Ruby on Rails. (I first looked to see if Elixir or Erlang had such a thing, but apparently not.) Great illustration of the importance of exposure to lots of different languages (which is one reason I'm doing this track in the first place), frameworks, etc.

I think that make_noises(number) || Integer.to_string(number)

looks very JavaScript-ish.

Just joking, very nice solution. I didn't know I could use and or operator (||) like that. And I love that @noises name.

### What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

• What compromises have been made?