Skip to content

Proverb

Build the “For want of a nail” proverb from any chain of things, one line per link and a closing line for the first.

Adapted from Exercism’s problem specification (MIT). Listed under Strings & Text.

// FILE: proverb.nomi
//# The "for want of a nail" proverb, built from any chain of things.

/// The proverb's lines for `pieces`: one line for each piece and the piece
/// after it, then a closing line for the first piece. No pieces, no lines.
pub fn recite(pieces: List<String>) -> List<String>
    [first, ..] = pieces else return []

    Iter.zip(pieces, Iter.drop(pieces, 1))
    |> Iter.map(fn (wanted, lost) -> "For want of a ${wanted} the ${lost} was lost.")
    |> Iter.concat(["And all for the want of a ${first}."])
    |> Iter.to_list()
end

// FILE: proverb_test.nomi
import proverb

test "zero pieces"
    assert proverb.recite([]) == []
end

test "one piece"
    assert proverb.recite(["nail"]) == ["And all for the want of a nail."]
end

test "two pieces"
    assert proverb.recite(["nail", "shoe"]) == [
        "For want of a nail the shoe was lost.",
        "And all for the want of a nail.",
    ]
end

test "three pieces"
    assert proverb.recite(["nail", "shoe", "horse"]) == [
        "For want of a nail the shoe was lost.",
        "For want of a shoe the horse was lost.",
        "And all for the want of a nail.",
    ]
end

test "full proverb"
    assert proverb.recite(["nail", "shoe", "horse", "rider", "message", "battle", "kingdom"]) == [
        "For want of a nail the shoe was lost.",
        "For want of a shoe the horse was lost.",
        "For want of a horse the rider was lost.",
        "For want of a rider the message was lost.",
        "For want of a message the battle was lost.",
        "For want of a battle the kingdom was lost.",
        "And all for the want of a nail.",
    ]
end

test "four pieces modernized"
    assert proverb.recite(["pin", "gun", "soldier", "battle"]) == [
        "For want of a pin the gun was lost.",
        "For want of a gun the soldier was lost.",
        "For want of a soldier the battle was lost.",
        "And all for the want of a pin.",
    ]
end
  • recite returns the lines as a List<String>, as the canonical data does, rather than one string with newlines.
  • Neighbouring pieces are paired with Iter.zip(pieces, Iter.drop(pieces, 1)), which stops at the shorter side, so the last piece pairs with nothing. Iter.windows(pieces, 2) gives the same pairs as two-element Lists, but a List pattern is refutable, so the lambda could not take each pair apart in its parameter as it takes apart the tuple zip yields.
  • The closing line is added with Iter.concat as one more pipe stage, so the function is one guard and one pipeline.
  • The empty list is a binding else on [first, ..], which also binds the first piece the closing line names.
  • Each case is a plain test. A subtests table fits the shape, but the expected lists run to seven lines each, and a row per case read worse than a test per case.