exercism/elixir/bottle-song/lib/bottle_song.ex

29 lines
911 B
Elixir
Raw Normal View History

2024-07-01 18:35:51 +00:00
defmodule BottleSong do
@moduledoc """
Handles lyrics of the popular children song: Ten Green Bottles
"""
defp verse(n) do
bottle_sense = fn
0 -> "No green bottles"
1 -> "One green bottle"
2 -> "Two green bottles"
3 -> "Three green bottles"
4 -> "Four green bottles"
5 -> "Five green bottles"
6 -> "Six green bottles"
7 -> "Seven green bottles"
8 -> "Eight green bottles"
9 -> "Nine green bottles"
10 -> "Ten green bottles"
end
"#{bottle_sense.(n)} hanging on the wall,\n#{bottle_sense.(n)} hanging on the wall,\nAnd if one green bottle should accidentally fall,\nThere'll be #{String.downcase(bottle_sense.(n-1))} hanging on the wall."
end
@spec recite(pos_integer, pos_integer) :: String.t()
def recite(start_bottle, take_down) do
Enum.map_join(0..(take_down - 1), "\n\n", &verse(start_bottle - &1))
end
end