forked from exercism/nim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
exercises(allergies): overhaul to use enum (exercism#434)
This exercise is intended to practice bit manipulation. Make it somewhat more idiomatic by avoiding the seq of strings. Of course, for Nim code in the real world, we should use the built-in `set` type. The overall design of this exercise hadn't changed since the initial implementation in 7e55686 ("Adds allergies exercise", 2018-04-17). Closes: exercism#404
- Loading branch information
Showing
2 changed files
with
65 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,16 @@ | ||
import sequtils | ||
|
||
type | ||
Allergies* = object | ||
score*: int | ||
|
||
var | ||
allergiesList = [ | ||
"eggs", | ||
"peanuts", | ||
"shellfish", | ||
"strawberries", | ||
"tomatoes", | ||
"chocolate", | ||
"pollen", | ||
"cats" | ||
] | ||
|
||
Allergen* = enum | ||
Eggs | ||
Peanuts | ||
Shellfish | ||
Strawberries | ||
Tomatoes | ||
Chocolate | ||
Pollen | ||
Cats | ||
|
||
proc isAllergicTo*(allergies: Allergies, allergy: string): bool = | ||
(allergies.score and 1 shl allergiesList.find(allergy)) != 0 | ||
func isAllergicTo*(score: int, allergen: Allergen): bool = | ||
(score and 1 shl allergen.ord) != 0 | ||
|
||
proc lst*(allergies: Allergies): seq[string] = | ||
allergiesList.filterIt(allergies.isAllergicTo(it)) | ||
func allergies*(score: int): set[Allergen] = | ||
cast[typeof(result)](score) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters