September 2017
Beginner
402 pages
9h 52m
English
The example with a factorial can generate numbers as long as the memory limitations of the computer allow. Although we may want to calculate, say a factorial of 100, the program will not do that until we really need the value. No computational resources are spent if the result is not needed yet. This is the idea behind lazy calculations.
In Perl 6, the ... operator creates a sequence. The simplest case looks similar to how the range is created. A regular array will be created in the next example:
my @a = 1...100;say @a.elems;
The @a array is created immediately and it gets all the 100 elements, which are integers from 1 to 100:
say @a[0]; # 1say @a[1]; # 2say @a[98]; # 99say @a[99]; # 100
Contrarily, a lazy sequence ...
Read now
Unlock full access