Allergies
Decode an allergy score into the allergens it includes.
Adapted from Exercism’s problem specification (MIT). Listed under Numbers & Math.
// FILE: allergies.nomi
//# The allergies an allergy score stands for, one bit per allergen.
/// The allergens a score covers, in score order: eggs are worth 1, peanuts 2,
/// and each one after twice the one before.
pub enum Allergen
Eggs
Peanuts
Shellfish
Strawberries
Tomatoes
Chocolate
Pollen
Cats
end
derive Variants for Allergen
/// Reports whether `score` includes `allergen`.
pub fn is_allergic_to(score: Int, allergen: Allergen) -> Bool
score &&& value(allergen) != 0
end
/// Lists every allergen `score` includes, in score order. Parts of the score
/// above the last allergen are ignored.
pub fn list(score: Int) -> List<Allergen>
Allergen.variants()
|> Iter.filter(&is_allergic_to(score, &1))
|> Iter.to_list()
end
fn value(allergen: Allergen) -> Int
case allergen
.Eggs -> 1
.Peanuts -> 2
.Shellfish -> 4
.Strawberries -> 8
.Tomatoes -> 16
.Chocolate -> 32
.Pollen -> 64
.Cats -> 128
end
end
// FILE: allergies_test.nomi
import
allergies
allergies.Allergen
end
test "testing for eggs allergy"
[
("not allergic to anything", 0, False),
("allergic only to eggs", 1, True),
("allergic to eggs and something else", 3, True),
("allergic to something, but not eggs", 2, False),
("allergic to everything", 255, True),
]
|> subtests (description, score, expected), description
assert allergies.is_allergic_to(score, .Eggs) == expected
end
end
test "testing for peanuts allergy"
[
("not allergic to anything", 0, False),
("allergic only to peanuts", 2, True),
("allergic to peanuts and something else", 7, True),
("allergic to something, but not peanuts", 5, False),
("allergic to everything", 255, True),
]
|> subtests (description, score, expected), description
assert allergies.is_allergic_to(score, .Peanuts) == expected
end
end
test "testing for shellfish allergy"
[
("not allergic to anything", 0, False),
("allergic only to shellfish", 4, True),
("allergic to shellfish and something else", 14, True),
("allergic to something, but not shellfish", 10, False),
("allergic to everything", 255, True),
]
|> subtests (description, score, expected), description
assert allergies.is_allergic_to(score, .Shellfish) == expected
end
end
test "testing for strawberries allergy"
[
("not allergic to anything", 0, False),
("allergic only to strawberries", 8, True),
("allergic to strawberries and something else", 28, True),
("allergic to something, but not strawberries", 20, False),
("allergic to everything", 255, True),
]
|> subtests (description, score, expected), description
assert allergies.is_allergic_to(score, .Strawberries) == expected
end
end
test "testing for tomatoes allergy"
[
("not allergic to anything", 0, False),
("allergic only to tomatoes", 16, True),
("allergic to tomatoes and something else", 56, True),
("allergic to something, but not tomatoes", 40, False),
("allergic to everything", 255, True),
]
|> subtests (description, score, expected), description
assert allergies.is_allergic_to(score, .Tomatoes) == expected
end
end
test "testing for chocolate allergy"
[
("not allergic to anything", 0, False),
("allergic only to chocolate", 32, True),
("allergic to chocolate and something else", 112, True),
("allergic to something, but not chocolate", 80, False),
("allergic to everything", 255, True),
]
|> subtests (description, score, expected), description
assert allergies.is_allergic_to(score, .Chocolate) == expected
end
end
test "testing for pollen allergy"
[
("not allergic to anything", 0, False),
("allergic only to pollen", 64, True),
("allergic to pollen and something else", 224, True),
("allergic to something, but not pollen", 160, False),
("allergic to everything", 255, True),
]
|> subtests (description, score, expected), description
assert allergies.is_allergic_to(score, .Pollen) == expected
end
end
test "testing for cats allergy"
[
("not allergic to anything", 0, False),
("allergic only to cats", 128, True),
("allergic to cats and something else", 192, True),
("allergic to something, but not cats", 64, False),
("allergic to everything", 255, True),
]
|> subtests (description, score, expected), description
assert allergies.is_allergic_to(score, .Cats) == expected
end
end
test "list when:"
[
("no allergies", 0, []),
("just eggs", 1, [Allergen.Eggs]),
("just peanuts", 2, [.Peanuts]),
("just strawberries", 8, [.Strawberries]),
("eggs and peanuts", 3, [.Eggs, .Peanuts]),
("more than eggs but not peanuts", 5, [.Eggs, .Shellfish]),
("lots of stuff", 248, [.Strawberries, .Tomatoes, .Chocolate, .Pollen, .Cats]),
(
"everything",
255,
[.Eggs, .Peanuts, .Shellfish, .Strawberries, .Tomatoes, .Chocolate, .Pollen, .Cats],
),
(
"no allergen score parts",
509,
[.Eggs, .Shellfish, .Strawberries, .Tomatoes, .Chocolate, .Pollen, .Cats],
),
("no allergen score parts without highest valid score", 257, [.Eggs]),
]
|> subtests (description, score, expected), description
assert allergies.list(score) == expected
end
endok testing for eggs allergy (5 cases)
ok testing for peanuts allergy (5 cases)
ok testing for shellfish allergy (5 cases)
ok testing for strawberries allergy (5 cases)
ok testing for tomatoes allergy (5 cases)
ok testing for chocolate allergy (5 cases)
ok testing for pollen allergy (5 cases)
ok testing for cats allergy (5 cases)
ok list when: (10 cases)
test result: ok. 50 passed, 0 failed
How it’s written
Section titled “How it’s written”- Allergens are an enum, and
derive VariantsgivesAllergen.variants()in declaration order, which is the score orderlistmust return them in. - Each allergen’s score is spelled out in a
case(value), rather than computed as1 <<< indexfrom its position invariants(). The table is the problem statement, and reordering the enum cannot silently change it. score &&& value(allergen) != 0tests the allergen’s bit:&&&binds tighter than!=, as*does, so it needs no parentheses.is_allergic_totakes the score first, solistreadsIter.filter(&is_allergic_to(score, &1)), a capture.- The canonical
itemstrings become variants in the tests (Allergen.Eggs); there is no string parsing in the API. - The
listtable names the enum in its first row that needs it,[Allergen.Eggs], and the rows after it write bare variants (.Peanuts), so the table needs no type annotation.