exercism/elixir/twelve-days/lib/twelve_days.ex

38 lines
3.2 KiB
Elixir
Raw Normal View History

2024-08-22 00:36:51 +00:00
defmodule TwelveDays do
@doc """
Given a `number`, return the song's verse for that specific day, including
all gifts for previous days in the same line.
"""
@spec verse(number :: integer) :: String.t()
def verse(1), do: "On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree."
def verse(2), do: "On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree."
def verse(3), do: "On the third day of Christmas my true love gave to me: three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(4), do: "On the fourth day of Christmas my true love gave to me: four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(5), do: "On the fifth day of Christmas my true love gave to me: five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(6), do: "On the sixth day of Christmas my true love gave to me: six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(7), do: "On the seventh day of Christmas my true love gave to me: seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(8), do: "On the eighth day of Christmas my true love gave to me: eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(9), do: "On the ninth day of Christmas my true love gave to me: nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(10), do: "On the tenth day of Christmas my true love gave to me: ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(11), do: "On the eleventh day of Christmas my true love gave to me: eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
def verse(12), do: "On the twelfth day of Christmas my true love gave to me: twelve Drummers Drumming, eleven Pipers Piping, ten Lords-a-Leaping, nine Ladies Dancing, eight Maids-a-Milking, seven Swans-a-Swimming, six Geese-a-Laying, five Gold Rings, four Calling Birds, three French Hens, two Turtle Doves, and a Partridge in a Pear Tree."
@doc """
Given a `starting_verse` and an `ending_verse`, return the verses for each
included day, one per line.
"""
@spec verses(starting_verse :: integer, ending_verse :: integer) :: String.t()
def verses(starting_verse, ending_verse) do
starting_verse..ending_verse
|> Enum.map(&verse/1)
|> Enum.join("\n")
end
@doc """
Sing all 12 verses, in order, one verse per line.
"""
@spec sing() :: String.t()
def sing(), do: verses(1, 12)
end