Skip to content

Commit b856651

Browse files
committed
Clarifying the dual Iterable/Iterator roles in a lazy contest refs #2139
1 parent a7691be commit b856651

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

doc/Language/list.pod6

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -232,30 +232,35 @@ value, but unlike the above options, it will break L<Scalars|/type/Scalar>.
232232
X<|laziness in iterable objects>
233233
=head1 Lazy lists
234234
235-
Lists, arrays, sequences and any class with the L<Iterator> role can be lazy,
236-
which means that their values are computed on demand and stored for later use.
237-
To create a lazy object use L<gather/take|/language/control#gather/take> or the
238-
L<sequence operator|/language/operators#infix_...>. You can also write a class
239-
that implements the role L<Iterator|/type/Iterator> and returns C<True> on a
240-
call to L<is-lazy|/routine/is-lazy>. Please note that some methods like C<elems>
241-
cannot be called on a lazy List and will result in a thrown
235+
C<List>s, C<Seq>s (and any class that subclasses them, like C<Array>s) and
236+
any other class thet implements the L<Iterable> role can be lazy, which means that their
237+
values are computed on demand and stored for later use. To create a lazy object
238+
use L<gather/take|/language/control#gather/take> or the L<sequence
239+
operator|/language/operators#infix_...>. You can also write a class that
240+
implements the role L<Iterator|/type/Iterator> and returns C<True> on a call to
241+
L<is-lazy|/routine/is-lazy>. Please note that some methods like C<elems> cannot
242+
be called on a lazy List and will result in a thrown
242243
L<Exception|/type/Exception>.
243244
244-
# This list is lazy and elements will not be available
245+
# This array is lazy and its elements will not be available
245246
# until explicitly requested.
246247
247-
my @l = lazy 1, 11, 121 ... 10**100;
248-
say @l.is-lazy; # OUTPUT: «True␤»
249-
say @l[]; # OUTPUT: «[...]␤»
248+
my @lazy-array = lazy 1, 11, 121 ... 10**100;
249+
say @lazy-array.is-lazy; # OUTPUT: «True␤»
250+
say @lazy-array[]; # OUTPUT: «[...]␤»
250251
251252
# Once all elements have been retrieved, the List
252253
# is no longer considered lazy.
253254
254-
my @no-longer-lazy = eager @l; # Forcing eager evaluation
255+
my @no-longer-lazy = eager @lazy-array; # Forcing eager evaluation
255256
say @no-longer-lazy.is-lazy; # OUTPUT: «False␤»
256257
say @no-longer-lazy[];
257258
# OUTPUT: (sequence starting with «[1 11 121» ending with a 300 digit number)
258259
260+
In the example above, C<@lazy-array> is an C<Array> which, through construction,
261+
is make C<lazy>. When using it with C<is-lazy> it is returning an iterator,
262+
which, since it originates in a lazy list, is itself lazy.
263+
259264
A common use case for lazy C<List>s is the processing of infinite sequences of
260265
numbers, whose values have not been computed yet and cannot be computed in their
261266
entirety. Specific values in the List will only be computed when they are

0 commit comments

Comments
 (0)