Skip to content

Instantly share code, notes, and snippets.

@djspinmonkey
Created November 29, 2016 00:10
Show Gist options
  • Save djspinmonkey/c8ae9d02969f1fac14a28d48834ed7d7 to your computer and use it in GitHub Desktop.
Save djspinmonkey/c8ae9d02969f1fac14a28d48834ed7d7 to your computer and use it in GitHub Desktop.
defmodule Bottles do
def song do
verses(99, 0)
end
def verses(first, last) do
first..last
|> Enum.map_join("\n", &verse(&1))
end
def verse(n) do
"#{bottles(n, :capital)} of beer on the wall, #{bottles(n)} of beer.\n" <>
case n do
0 -> "Go to the store and buy some more, 99 bottles of beer on the wall.\n"
1 -> "Take it down and pass it around, no more bottles of beer on the wall.\n"
_ -> "Take one down and pass it around, #{bottles(n-1)} of beer on the wall.\n"
end
end
defp bottles(n, case \\ :lower)
defp bottles(0, :capital), do: "No more bottles"
defp bottles(0, :lower), do: "no more bottles"
defp bottles(1, _), do: "1 bottle"
defp bottles(n, _), do: "#{n} bottles"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment