Created
July 23, 2016 20:30
-
-
Save aaronjensen/39b08f9a4e7d4749fb1e76aa345a8c23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 song, do: verses(99, 0) | |
| def verses(first, last) do | |
| Enum.map_join first..last, "\n", &verse/1 | |
| end | |
| def verse(number) do | |
| "#{String.capitalize(containers(number))} of beer on the wall, " <> | |
| "#{containers(number)} of beer.\n" <> | |
| "#{action(number)}, " <> | |
| "#{containers(successor(number))} of beer on the wall.\n" | |
| end | |
| defp containers(6), do: "1 six-pack" | |
| defp containers(number), do: "#{quantity(number)} #{bottles(number)}" | |
| defp quantity(0), do: "no more" | |
| defp quantity(number), do: to_string number | |
| defp bottles(1), do: "bottle" | |
| defp bottles(_number), do: "bottles" | |
| defp action(0), do: "Go to the store and buy some more" | |
| defp action(number), do: "Take #{pronoun(number)} down and pass it around" | |
| defp pronoun(1), do: "it" | |
| defp pronoun(_number), do: "one" | |
| defp successor(0), do: 99 | |
| defp successor(number), do: number - 1 | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment