Course of Raku / Advanced / More about built-in types / Enumerations 🆕 / Exercises / Weekday numbers
Solution: Weekday numbers
Here is a possible solution to the task.
Code
enum Day (Mon => 1, Tue => 2, Wed => 3, Thu => 4, Fri => 5);
say Day(5);
say Day(1);🦋 You can find the source code in the file weekday-values.raku.
Output
Fri
MonComments
Writing the constants as pairs lets you choose the numbers, here starting from
1.Calling the enum type as
Day(5)performs the reverse lookup: it finds the constant whose value is5, which isFri. LikewiseDay(1)givesMon. This is the inverse ofFri.value, which would go from the name to the number.