Built-in routines

Laziness — lazy, eager & infinite lists

Full

Work with lists that compute elements on demand, including infinite ones.

Many Raku list producers are lazy: they compute elements only as you ask for them. That makes infinite lists ordinary values — you can define "all the squares" or "all the primes" and then take just the first few. A lazy list does no work until something pulls on it, so an unbounded source never runs away.

Taking from an infinite list #

An infinite range like 1..Inf (or 1..∞) is lazy; mapping or grepping over it stays lazy, so indexing or .head only forces the elements you actually read.

my @squares = (1..Inf).map(* ** 2);
say @squares[^5];
say (1..Inf).grep(*.is-prime).head(5);
Output
(1 4 9 16 25)
(2 3 5 7 11)

Neither line computes more than it needs: @squares[^5] forces five squares, and .head(5) stops the prime filter after the fifth prime.

Testing laziness — .is-lazy #

.is-lazy reports whether a list still has unrealised, on-demand elements. A bounded range knows its size and is not lazy; an unbounded one is.

say (1..10).is-lazy;
say (1..Inf).is-lazy;
Output
False
True

Finding without a bound #

Because the search is lazy, .first can scan an infinite list and stop at the first hit — no upper limit needed.

say (1..Inf).first(* > 1000);
Output
1001

Self-referential sequences #

The sequence operator ... (see The sequence operator) builds a lazy series from a closure — even one that reads its own earlier terms, like Fibonacci — and you take a prefix the same way.

my \fib = (1, 1, * + * ... *);
say fib[^10];
Output
(1 1 2 3 5 8 13 21 34 55)

Notes #