Bottle Song
Recite the counting song “Ten Green Bottles”, from a given number of bottles down, for a given number of verses.
Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.
// FILE: bottle_song.nomi
//# The counting song "Ten Green Bottles".
/// The words for zero to ten bottles, as the song sings them.
once numbers = Vector[
"no",
"one",
"two",
"three",
"four",
"five",
"six",
"seven",
"eight",
"nine",
"ten",
]
/// The lines of `take_down` verses, starting from `start_bottles` bottles,
/// with an empty line between two verses.
pub fn recite(start_bottles: Int, take_down: Int) -> List<String>
start_bottles
|> Iter.iterate(&(&1 - 1))
|> Iter.take(take_down)
|> Iter.map(verse)
|> Iter.intersperse([""])
|> Iter.flatten()
|> Iter.to_list()
end
/// The verse sung while `bottles` bottles hang on the wall.
fn verse(bottles: Int) -> List<String>
count = green_bottles(bottles) |> String.capitalize()
hanging = "${count} hanging on the wall,"
[
hanging,
hanging,
"And if one green bottle should accidentally fall,",
"There'll be ${green_bottles(bottles - 1)} hanging on the wall.",
]
end
/// `bottles` counted in words: "one green bottle", "two green bottles".
fn green_bottles(bottles: Int) -> String
number = Vector.get(numbers, bottles)!
if bottles == 1 then
"${number} green bottle"
else
"${number} green bottles"
end
end
// FILE: bottle_song_test.nomi
import bottle_song
test "single verse"
[
(
"first generic verse",
10,
1,
[
"Ten green bottles hanging on the wall,",
"Ten green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be nine green bottles hanging on the wall.",
],
),
(
"last generic verse",
3,
1,
[
"Three green bottles hanging on the wall,",
"Three green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be two green bottles hanging on the wall.",
],
),
(
"verse with 2 bottles",
2,
1,
[
"Two green bottles hanging on the wall,",
"Two green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be one green bottle hanging on the wall.",
],
),
(
"verse with 1 bottle",
1,
1,
[
"One green bottle hanging on the wall,",
"One green bottle hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be no green bottles hanging on the wall.",
],
),
]
|> subtests (description, start_bottles, take_down, expected), description
assert bottle_song.recite(start_bottles, take_down) == expected
end
end
test "multiple verses"
[
(
"first two verses",
10,
2,
[
"Ten green bottles hanging on the wall,",
"Ten green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be nine green bottles hanging on the wall.",
"",
"Nine green bottles hanging on the wall,",
"Nine green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be eight green bottles hanging on the wall.",
],
),
(
"last three verses",
3,
3,
[
"Three green bottles hanging on the wall,",
"Three green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be two green bottles hanging on the wall.",
"",
"Two green bottles hanging on the wall,",
"Two green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be one green bottle hanging on the wall.",
"",
"One green bottle hanging on the wall,",
"One green bottle hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be no green bottles hanging on the wall.",
],
),
(
"all verses",
10,
10,
[
"Ten green bottles hanging on the wall,",
"Ten green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be nine green bottles hanging on the wall.",
"",
"Nine green bottles hanging on the wall,",
"Nine green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be eight green bottles hanging on the wall.",
"",
"Eight green bottles hanging on the wall,",
"Eight green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be seven green bottles hanging on the wall.",
"",
"Seven green bottles hanging on the wall,",
"Seven green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be six green bottles hanging on the wall.",
"",
"Six green bottles hanging on the wall,",
"Six green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be five green bottles hanging on the wall.",
"",
"Five green bottles hanging on the wall,",
"Five green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be four green bottles hanging on the wall.",
"",
"Four green bottles hanging on the wall,",
"Four green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be three green bottles hanging on the wall.",
"",
"Three green bottles hanging on the wall,",
"Three green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be two green bottles hanging on the wall.",
"",
"Two green bottles hanging on the wall,",
"Two green bottles hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be one green bottle hanging on the wall.",
"",
"One green bottle hanging on the wall,",
"One green bottle hanging on the wall,",
"And if one green bottle should accidentally fall,",
"There'll be no green bottles hanging on the wall.",
],
),
]
|> subtests (description, start_bottles, take_down, expected), description
assert bottle_song.recite(start_bottles, take_down) == expected
end
endok single verse (4 cases)
ok multiple verses (3 cases)
test result: ok. 7 passed, 0 failed
How it’s written
Section titled “How it’s written”- The numbers are a
Vectorof their words from “no” to “ten”, read by the bottle count, andgreen_bottlesadds “green bottle” or “green bottles”. A verse’s first two lines start with that phrase capitalized, and its last line uses the phrase for one bottle fewer, so “no green bottles” and “one green bottle” come out of the same two functions as every other count. - The counts sung are
Iter.iteratedown from the start, takentake_downtimes. recitereturns lines, as the canonical data does, with an empty line between two verses:Iter.intersperse([""])puts a verse of one empty line between each two verses, andIter.flattenjoins them.- The canonical groups, single verses and multiple verses, are one test each, holding one
subteststable of the canonical lines written out.