Skip to content

Two-fer

Say who gets one of two cookies: “One for Alice, one for me.”, or “you” when no name is given.

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

// FILE: two_fer.nomi
//# "One for you, one for me."

/// Says who gets the cookie: "One for Alice, one for me."
pub fn two_fer(name: String = "you") -> String
    "One for ${name}, one for me."
end

// FILE: two_fer_test.nomi
import two_fer.two_fer

test "no name given"
    assert two_fer() == "One for you, one for me."
end

test "a name given"
    assert two_fer("Alice") == "One for Alice, one for me."
end

test "another name given"
    assert two_fer("Bob") == "One for Bob, one for me."
end
  • The missing name is a parameter default, name: String = "you", rather than a Maybe<String>. The canonical null means “called without a name”, and a default says that at the call site: two_fer().
  • Three plain test blocks rather than a subtests table: the first case calls with no argument, so the rows do not share a shape.