Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JJ committed Jul 3, 2018
1 parent b856651 commit f3052b8
Showing 1 changed file with 18 additions and 11 deletions.
29 changes: 18 additions & 11 deletions doc/Language/list.pod6
Expand Up @@ -229,13 +229,13 @@ value, but unlike the above options, it will break L<Scalars|/type/Scalar>.
say (1, |$(2, 3), 4) eqv (1, 2, 3, 4); # OUTPUT: «True␤»
say (1, slip($(2, 3)), 4) eqv (1, 2, 3, 4); # OUTPUT: «False␤»
X<|laziness in iterable objects>
X<|laziness in Iterable objects>
=head1 Lazy lists
C<List>s, C<Seq>s (and any class that subclasses them, like C<Array>s) and
any other class thet implements the L<Iterable> role can be lazy, which means that their
values are computed on demand and stored for later use. To create a lazy object
use L<gather/take|/language/control#gather/take> or the L<sequence
C<List>s, C<Seq>s (and any class that subclasses them, like C<Array>s) and any
other class thet implements the L<Iterable> role can be lazy, which means that
their values are computed on demand and stored for later use. To create a lazy
object use L<gather/take|/language/control#gather/take> or the L<sequence
operator|/language/operators#infix_...>. You can also write a class that
implements the role L<Iterator|/type/Iterator> and returns C<True> on a call to
L<is-lazy|/routine/is-lazy>. Please note that some methods like C<elems> cannot
Expand All @@ -252,24 +252,31 @@ L<Exception|/type/Exception>.
# Once all elements have been retrieved, the List
# is no longer considered lazy.
my @no-longer-lazy = eager @lazy-array; # Forcing eager evaluation
my @no-longer-lazy = eager @lazy-array; # Forcing eager evaluation
say @no-longer-lazy.is-lazy; # OUTPUT: «False␤»
say @no-longer-lazy[];
# OUTPUT: (sequence starting with «[1 11 121» ending with a 300 digit number)
In the example above, C<@lazy-array> is an C<Array> which, through construction,
is make C<lazy>. When using it with C<is-lazy> it is returning an iterator,
is made C<lazy>. When calling C<is-lazy> on it it is returning an C<Iterator>,
which, since it originates in a lazy list, is itself lazy.
A common use case for lazy C<List>s is the processing of infinite sequences of
A common use case for lazy C<Seq>s is the processing of infinite sequences of
numbers, whose values have not been computed yet and cannot be computed in their
entirety. Specific values in the List will only be computed when they are
needed.
my @l = 1, 2, 4, 8 ... Inf;
say @l[0..16];
my $l := 1, 2, 4, 8 ... Inf;
say $l[0..16];
# OUTPUT: «(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536)␤»
You can easily assign lazy objects to other objects, conserving their laziness:
my $l := 1, 2, 4, 8 ... Inf; # This is a lazy Seq.
my @lazy-array = $l;
say @lazy-array[10..15]; # OUTPUT: «(1024 2048 4096 8192 16384 32768)␤»
say @lazy-array.is-lazy; # OUTPUT: «True␤»
=head1 Immutability
The lists we have talked about so far (C<List>, C<Seq> and C<Slip>)
Expand All @@ -289,7 +296,7 @@ can still change the value which that C<Scalar> points to:
(1, $a, 3)[1] = 42;
$a.say; # OUTPUT: «42␤»
...that is, it is only the list structure itself – how many elements there are
that is, it is only the list structure itself – how many elements there are
and each element's identity – that is immutable. The immutability is not
contagious past the identity of the element.
Expand Down

0 comments on commit f3052b8

Please sign in to comment.