Twelve Days
Recite the verses of “The Twelve Days of Christmas”, each adding a gift to the one before.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: twelve_days.nomi
//# The lyrics of "The Twelve Days of Christmas".
once ordinals = Vector[
"first",
"second",
"third",
"fourth",
"fifth",
"sixth",
"seventh",
"eighth",
"ninth",
"tenth",
"eleventh",
"twelfth",
]
once first_gift = "a Partridge in a Pear Tree"
/// The gifts of the second day to the twelfth, in order.
once later_gifts = [
"two Turtle Doves",
"three French Hens",
"four Calling Birds",
"five Gold Rings",
"six Geese-a-Laying",
"seven Swans-a-Swimming",
"eight Maids-a-Milking",
"nine Ladies Dancing",
"ten Lords-a-Leaping",
"eleven Pipers Piping",
"twelve Drummers Drumming",
]
/// The verses for days `start` to `end`, both included, counting from 1.
pub fn recite(start: Int, stop: Int) -> List<String>
start..=stop
|> Iter.map(verse)
|> Iter.to_list()
end
fn verse(day: Int) -> String
ordinal = Vector.get(ordinals, day - 1)!
"On the ${ordinal} day of Christmas my true love gave to me: ${gifts(day)}."
end
/// Every gift given on `day`, newest first.
fn gifts(day: Int) -> String
if day == 1 then return first_gift
later =
later_gifts
|> Iter.take(day - 1)
|> Iter.reverse()
|> String.join(", ")
"${later}, and ${first_gift}"
end
// FILE: twelve_days_test.nomi
import twelve_days
/// Each canonical verse on its own: its description, its number and its text.
once verses = [
(
"first day a partridge in a pear tree",
1,
"On the first day of Christmas my true love gave to me: a Partridge in a Pear Tree.",
),
(
"second day two turtle doves",
2,
"On the second day of Christmas my true love gave to me: two Turtle Doves, and a Partridge in a Pear Tree.",
),
(
"third day three french hens",
3,
"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.",
),
(
"fourth day four calling birds",
4,
"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.",
),
(
"fifth day five gold rings",
5,
"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.",
),
(
"sixth day six geese-a-laying",
6,
"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.",
),
(
"seventh day seven swans-a-swimming",
7,
"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.",
),
(
"eighth day eight maids-a-milking",
8,
"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.",
),
(
"ninth day nine ladies dancing",
9,
"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.",
),
(
"tenth day ten lords-a-leaping",
10,
"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.",
),
(
"eleventh day eleven pipers piping",
11,
"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.",
),
(
"twelfth day twelve drummers drumming",
12,
"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.",
),
]
test "verse"
subtests verses, (description, number, expected), description
assert twelve_days.recite(number, number) == [expected]
end
end
test "lyrics"
[
("recites first three verses of the song", 1, 3),
("recites three verses from the middle of the song", 4, 6),
("recites the whole song", 1, 12),
]
|> subtests (description, start, stop), description
expected =
verses
|> Iter.drop(start - 1)
|> Iter.take(stop - start + 1)
|> Iter.map(fn (_description, _number, verse) -> verse)
|> Iter.to_list()
assert twelve_days.recite(start, stop) == expected
end
endok verse (12 cases)
ok lyrics (3 cases)
test result: ok. 15 passed, 0 failed
How it’s written
Section titled “How it’s written”- The ordinals are a
Vectorread withVector.get(...)!: the day comes fromrecite’s range, so an index out of bounds is a caller’s bug, and a trap reports it (style §29). - The first day’s gift is a
onceof its own, apart from the later gifts, because it is the only one that changes: it gains “and” once another gift comes before it.giftsreturns it alone on day one, and otherwise takes the later gifts, reverses them and joins them before ”, and a Partridge in a Pear Tree”. String.join_listwith an English locale was the other way to write the “and”, but CLDR’s English list puts no comma before “and” in a list of two, and the song does.- The test file holds each canonical verse once, in a
once versestable. The single-verse cases are asubtestsover it, and the three multi-verse cases take their expected verses from it withIter.dropandIter.take, since each canonical multi-verse case is exactly those verses in order. Writing the whole song out again for “recites the whole song” would add 40 lines of the same text.