Documentation for routine
sum assembled from the following types:
sub sum( --> Numeric)method sum(List: --> Numeric)
Returns the sum of all elements in the list or 0 if the list is empty. Throws an exception if an element can not be coerced into Numeric.
say (1, 3, pi).sum; # OUTPUT: «7.14159265358979␤»say (1, "0xff").sum; # OUTPUT: «256␤»say sum(0b1111, 5); # OUTPUT: «20␤»
When being called on native integer arrays, it is also possible to specify a
:wrap named parameter. This will add the values as native integers, wrapping around if they exceed the size of a native integer. If you are sure you will not exceed that value, or if you don't mind, using
:wrap will make the calculation about 20x as fast.
my int = ^1_000_000;say .sum(:wrap); # OUTPUT: «499999500000␤»
multi method sum(--> Numeric)
Returns the sum of all elements in the Range. Throws X::Str::Numeric if an element can not be coerced into Numeric.
(1..10).sum # 55