Documentation for :cached
cached assembled from the following types:
The following pragma:
use experimental :cached;
turns on the
is cached trait, which stores the result of a routine call, returning the same value if called with the same arguments.
It can be used when heavy calculations are involved, as in this sample that uses amicable numbers, taken from the 2018 Advent Calendar:
use experimental :cached;sub aliquot-parts( ) is cachedsub infix:<amic>( , )# Taken frommy = [2620, 2924, 5020, 5564, 6232, 6368, 66928, 66992];say "Aliquot parts of $_ are ", aliquot-parts for ;for X ->
This code caches the computation of the aliquot parts, so that when the
amic operator is called, it's only computed once; as a matter of fact, the first loop which prints these aliquot parts will be the only one that actually perform the computation.
See also the description of the trait for additional information and examples.