Skip to content
Permalink
Browse files

Clarifying roles and reflowing.

Closes #2139
  • Loading branch information...
JJ committed May 24, 2019
1 parent d60e8a0 commit bb48e46478428b56a71f1c09d06f69f8d2b21651
Showing with 27 additions and 24 deletions.
  1. +27 −24 doc/Language/list.pod6
@@ -289,43 +289,46 @@ value, but unlike the above options, it will break L<Scalars|/type/Scalar>.
X<|laziness in Iterable objects>
=head1 Lazy lists
C<List>s, C<Seq>s, C<Array>s and any
other class that implements the L<Iterable|/type/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 be called on a lazy List and will result in a thrown
C<List>s, C<Seq>s, C<Array>s and any other class that implements the
L<Iterator|/type/Iterator> role can be lazy, which means that their values are
computed on demand and stored for later use. One of the ways to create a lazy
object is to 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
be called on a lazy List and will result in a thrown
L<Exception|/type/Exception>.
# This array is lazy and its elements will not be available
# until explicitly requested.
=begin code
# This array is lazy and its elements will not be available
# until explicitly requested.
my @lazy-array = lazy 1, 11, 121 ... 10**100;
say @lazy-array.is-lazy; # OUTPUT: «True␤»
say @lazy-array[]; # OUTPUT: «[...]␤»
my @lazy-array = lazy 1, 11, 121 ... 10**100;
say @lazy-array.is-lazy; # OUTPUT: «True␤»
say @lazy-array[]; # OUTPUT: «[...]␤»
# Once all elements have been retrieved, the List
# is no longer considered lazy.
# Once all elements have been retrieved, the list
# is no longer considered lazy.
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)
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)
=end code
In the example above, C<@lazy-array> is an C<Array> which, through construction,
is made C<lazy>. Calling C<is-lazy> on it returns an C<Iterator>,
which, since it originates in a lazy list, is itself lazy.
is made C<lazy>. Calling C<is-lazy> on it actually calls the method mixed in by
the role C<Iterator>, which, since it originates in a lazy list, is itself lazy.
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];
# OUTPUT: «(1 2 4 8 16 32 64 128 256 512 1024 2048 4096 8192 16384 32768 65536)␤»
=for code
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:

0 comments on commit bb48e46

Please sign in to comment.
You can’t perform that action at this time.