Sha256: d38de265fd8f8d1e071ad5e6dcf6e101cee4b4a8942ca61bf59ec36db15637da

Contents?: true

Size: 1.83 KB

Versions: 69

Compression:

Stored size: 1.83 KB

Contents

defmodule TwelveDays do
  @days [
    {"a", "first", "Partridge in a Pear Tree"},
    {"two", "second", "Turtle Doves"},
    {"three", "third", "French Hens"},
    {"four", "fourth", "Calling Birds"},
    {"five", "fifth", "Gold Rings"},
    {"six", "sixth", "Geese-a-Laying"},
    {"seven", "seventh", "Swans-a-Swimming"},
    {"eight", "eighth", "Maids-a-Milking"},
    {"nine", "ninth", "Ladies Dancing"},
    {"ten", "tenth", "Lords-a-Leaping"},
    {"eleven", "eleventh", "Pipers Piping"},
    {"twelve", "twelfth", "Drummers Drumming"}
  ]

  for {{count, ordinal, gift}, number} <- @days |> Enum.with_index(1) do
    defp count(unquote(number)), do: unquote(count)
    defp ordinal(unquote(number)), do: unquote(ordinal)
    defp gift(unquote(number)), do: "#{count(unquote(number))} #{unquote(gift)}"
  end

  @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(number) do
    "On the #{ordinal(number)} day of Christmas my true love gave to me, #{
      gifts_for_day(number, [])
    }."
  end

  defp gifts_for_day(1, []), do: gift(1)

  defp gifts_for_day(1, results),
    do: ["and #{gift(1)}" | results] |> Enum.reverse() |> Enum.join(", ")

  defp gifts_for_day(number, results), do: gifts_for_day(number - 1, [gift(number) | results])

  @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
    |> Range.new(ending_verse)
    |> Enum.map_join("\n", &verse/1)
  end

  @doc """
  Sing all 12 verses, in order, one verse per line.
  """
  @spec sing() :: String.t()
  def sing, do: verses(1, 12)
end

Version data entries

69 entries across 69 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.179 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.178 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.177 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.176 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.175 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.174 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.173 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.172 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.171 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.170 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.169 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.167 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.166 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.165 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.164 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.163 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.162 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.161 tracks/elixir/exercises/twelve-days/example.exs
trackler-2.2.1.160 tracks/elixir/exercises/twelve-days/example.exs