-
-
Save ciaran/097dec82cf85d849954d30269d23504c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule Bottles do | |
def verses(from, to) do | |
Enum.map_join(from..to, "\n", &verse(&1)) | |
end | |
def verse(num_bottles) do | |
"#{String.capitalize bottles_of_beer_on_the_wall(num_bottles)} of beer on the wall, " <> | |
"#{bottles_of_beer_on_the_wall(num_bottles)} of beer.\n" <> | |
"#{action(num_bottles)}.\n" | |
end | |
def song do | |
verses(99, 0) | |
end | |
defp action(0), do: "Go to the store and buy some more, 99 bottles of beer on the wall" | |
defp action(n), do: "Take #{it_or_one(n)} down and pass it around, #{bottles_left(n)} of beer on the wall" | |
defp pluralise_bottle(1), do: "bottle" | |
defp pluralise_bottle(_), do: "bottles" | |
defp it_or_one(1), do: "it" | |
defp it_or_one(_), do: "one" | |
defp bottles_of_beer_on_the_wall(0), do: "no more bottles" | |
defp bottles_of_beer_on_the_wall(n), do: "#{n} #{pluralise_bottle(n)}" | |
defp bottles_left(1), do: "no more bottles" | |
defp bottles_left(2), do: "1 bottle" | |
defp bottles_left(n), do: "#{n-1} bottles" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment