Course of Raku / Advanced / More about built-in types / Enumerations 🆕 / Exercises / Count and total
Solution: Count and total
Here is a possible solution to the task.
Code
enum Coin (penny => 1, nickel => 5, dime => 10, quarter => 25);
say Coin.enums.elems;
say Coin.enums.values.sum;🦋 You can find the source code in the file enum-introspection.raku.
Output
4
41Comments
The constants are defined as pairs, so each one gets the value we chose rather than the automatic numbering from zero.
.enumsreturns a map from each constant name to its value..elemscounts the entries — there are four coins..valuespulls out just the numbers behind the names, and.sumadds them up:1 + 5 + 10 + 25is41.
Course navigation
← Count and total | Operators →