Documentation for method polymod

Documentation for method polymod, assembled from the following types:

class Int

From Int

(Int) method polymod

Defined as:

method polymod(Int:D: +@mods)

Returns a sequence of mod results corresponding to the divisors in @mods. The divisors are given from smallest "unit" to the largest (e.g. 60 seconds per minute, 60 minutes per hour) and the results are returned in the same way: from smallest to the largest (5 seconds, 4 minutes).

Returns one more item in the result than the number of given divisors. If the divisors are given as a lazy list, runs until the remainder is 0 or the list of divisors is exhausted. All divisors must be Ints, unless the method is called on a non-Int number.

my $seconds = 1 * 60*60*24 # days 
            + 3 * 60*60    # hours 
            + 4 * 60       # minutes 
            + 5;           # seconds 
 
say $seconds.polymod(6060);                # OUTPUT: «(5 4 27)␤» 
say $seconds.polymod(606024);            # OUTPUT: «(5 4 3 1)␤» 
 
say 120.polymod:      11010², 10³, 10⁴;  # OUTPUT: «(0 0 12 0 0 0)␤» 
say 120.polymod: lazy 11010², 10³, 10⁴;  # OUTPUT: «(0 0 12)␤» 
say 120.polymod:      11010² … ∞;        # OUTPUT: «(0 0 12)␤» 
 
say ⅔.polymod(⅓);                            # OUTPUT: «(0 2)␤» 
say 5.Rat.polymod(.3.2);                   # OUTPUT: «(0.2 0 80)␤» 

To illustrate how the Int, non-lazy version of polymod works, consider this code that implements it:

my $seconds = 2 * 60*60*24 # days 
            + 3 * 60*60    # hours 
            + 4 * 60       # minutes 
            + 5;           # seconds 
 
my @pieces;
for 606024 -> $divisor {
    @pieces.push: $seconds mod $divisor;
    $seconds div= $divisor
}
@pieces.push: $seconds;
 
say @pieces# OUTPUT: «[5 4 3 2]␤» 

For a more detailed discussion, see this blog post