-
Notifications
You must be signed in to change notification settings - Fork 0
/
beer_song.exs
28 lines (27 loc) · 859 Bytes
/
beer_song.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
defmodule BeerSong do
@doc """
Get a single verse of the beer song
"""
@spec verse(integer) :: String.t()
@on "on the wall"
def verse(n), do:
"""
#{String.capitalize(beers(n))} #{@on}, #{beers(n)}.
#{action(n)}, #{beers(n - 1)} #{@on}.
"""
defp beers(-1), do: beers(99)
defp beers(n), do: "#{how_many(n)} bottle#{plural(n)} of beer"
defp action(0), do: "Go to the store and buy some more"
defp action(n), do: "Take #{pronoun(n)} down and pass it around"
defp plural(1), do: ""
defp plural(_), do: "s"
defp pronoun(1), do: "it"
defp pronoun(_), do: "one"
defp how_many(0), do: "no more"
defp how_many(n), do: n
@doc """
Get the entire beer song for a given range of numbers of bottles.
"""
@spec lyrics(Range.t()) :: String.t()
def lyrics(range \\ 99..0), do: Enum.map_join(range, "\n", &verse/1)
end