Skip to content

Commit f3052b8

Browse files
committed
Adding examples of casting among lazyfiable objects
Which refs #2139 I won't close until we decide if we leave it here or move to the new Iterating page, since this not only refers to lists, but also to Seqs and Maps.
1 parent b856651 commit f3052b8

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

doc/Language/list.pod6

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -229,13 +229,13 @@ value, but unlike the above options, it will break L<Scalars|/type/Scalar>.
229229
say (1, |$(2, 3), 4) eqv (1, 2, 3, 4); # OUTPUT: «True␤»
230230
say (1, slip($(2, 3)), 4) eqv (1, 2, 3, 4); # OUTPUT: «False␤»
231231
232-
X<|laziness in iterable objects>
232+
X<|laziness in Iterable objects>
233233
=head1 Lazy lists
234234
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
235+
C<List>s, C<Seq>s (and any class that subclasses them, like C<Array>s) and any
236+
other class thet implements the L<Iterable> role can be lazy, which means that
237+
their values are computed on demand and stored for later use. To create a lazy
238+
object use L<gather/take|/language/control#gather/take> or the L<sequence
239239
operator|/language/operators#infix_...>. You can also write a class that
240240
implements the role L<Iterator|/type/Iterator> and returns C<True> on a call to
241241
L<is-lazy|/routine/is-lazy>. Please note that some methods like C<elems> cannot
@@ -252,24 +252,31 @@ L<Exception|/type/Exception>.
252252
# Once all elements have been retrieved, the List
253253
# is no longer considered lazy.
254254
255-
my @no-longer-lazy = eager @lazy-array; # Forcing eager evaluation
255+
my @no-longer-lazy = eager @lazy-array; # Forcing eager evaluation
256256
say @no-longer-lazy.is-lazy; # OUTPUT: «False␤»
257257
say @no-longer-lazy[];
258258
# OUTPUT: (sequence starting with «[1 11 121» ending with a 300 digit number)
259259
260260
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,
261+
is made C<lazy>. When calling C<is-lazy> on it it is returning an C<Iterator>,
262262
which, since it originates in a lazy list, is itself lazy.
263263
264-
A common use case for lazy C<List>s is the processing of infinite sequences of
264+
A common use case for lazy C<Seq>s is the processing of infinite sequences of
265265
numbers, whose values have not been computed yet and cannot be computed in their
266266
entirety. Specific values in the List will only be computed when they are
267267
needed.
268268
269-
my @l = 1, 2, 4, 8 ... Inf;
270-
say @l[0..16];
269+
my $l := 1, 2, 4, 8 ... Inf;
270+
say $l[0..16];
271271
# OUTPUT: «(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536)␤»
272272
273+
You can easily assign lazy objects to other objects, conserving their laziness:
274+
275+
my $l := 1, 2, 4, 8 ... Inf; # This is a lazy Seq.
276+
my @lazy-array = $l;
277+
say @lazy-array[10..15]; # OUTPUT: «(1024 2048 4096 8192 16384 32768)␤»
278+
say @lazy-array.is-lazy; # OUTPUT: «True␤»
279+
273280
=head1 Immutability
274281
275282
The lists we have talked about so far (C<List>, C<Seq> and C<Slip>)
@@ -289,7 +296,7 @@ can still change the value which that C<Scalar> points to:
289296
(1, $a, 3)[1] = 42;
290297
$a.say; # OUTPUT: «42␤»
291298
292-
...that is, it is only the list structure itself – how many elements there are
299+
that is, it is only the list structure itself – how many elements there are
293300
and each element's identity – that is immutable. The immutability is not
294301
contagious past the identity of the element.
295302

0 commit comments

Comments
 (0)