@@ -229,13 +229,13 @@ value, but unlike the above options, it will break L<Scalars|/type/Scalar>.
229
229
say (1, |$(2, 3), 4) eqv (1, 2, 3, 4); # OUTPUT: «True»
230
230
say (1, slip($(2, 3)), 4) eqv (1, 2, 3, 4); # OUTPUT: «False»
231
231
232
- X < |laziness in iterable objects >
232
+ X < |laziness in Iterable objects >
233
233
= head1 Lazy lists
234
234
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
239
239
operator|/language/operators#infix_... > . You can also write a class that
240
240
implements the role L < Iterator|/type/Iterator > and returns C < True > on a call to
241
241
L < is-lazy|/routine/is-lazy > . Please note that some methods like C < elems > cannot
@@ -252,24 +252,31 @@ L<Exception|/type/Exception>.
252
252
# Once all elements have been retrieved, the List
253
253
# is no longer considered lazy.
254
254
255
- my @no-longer-lazy = eager @lazy-array; # Forcing eager evaluation
255
+ my @no-longer-lazy = eager @lazy-array; # Forcing eager evaluation
256
256
say @no-longer-lazy.is-lazy; # OUTPUT: «False»
257
257
say @no-longer-lazy[];
258
258
# OUTPUT: (sequence starting with «[1 11 121» ending with a 300 digit number)
259
259
260
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 ,
261
+ is made C < lazy > . When calling C < is-lazy > on it it is returning an C < Iterator > ,
262
262
which, since it originates in a lazy list, is itself lazy.
263
263
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
265
265
numbers, whose values have not been computed yet and cannot be computed in their
266
266
entirety. Specific values in the List will only be computed when they are
267
267
needed.
268
268
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];
271
271
# OUTPUT: «(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536)»
272
272
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
+
273
280
= head1 Immutability
274
281
275
282
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:
289
296
(1, $a, 3)[1] = 42;
290
297
$a.say; # OUTPUT: «42»
291
298
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
293
300
and each element's identity – that is immutable. The immutability is not
294
301
contagious past the identity of the element.
295
302
0 commit comments