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."
endok no name given
ok a name given
ok another name given
test result: ok. 3 passed, 0 failed
How it’s written
Section titled “How it’s written”- The missing name is a parameter default,
name: String = "you", rather than aMaybe<String>. The canonicalnullmeans “called without a name”, and a default says that at the call site:two_fer(). - Three plain
testblocks rather than asubteststable: the first case calls with no argument, so the rows do not share a shape.