Skip to content

Commit d19c977

Browse files
authored
Clarify that assigning a Seq to an Array consumes it
I thought the original sentence, "The same is true when assigning a Seq to an array." is not very clear on what's the same here. So I rewrote it and provided an example too.
1 parent a394425 commit d19c977

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

doc/Type/Seq.pod6

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@
88
99
A C<Seq> represents anything that can lazily produce a sequence of values. A
1010
C<Seq> is born in a state where iterating it will consume the values. However,
11-
calling .cache on a Seq will return a List that is still lazy, but stores the
12-
generated values for later access. The same is true when assigning a Seq to an
13-
array.
11+
calling C<.cache> on a C<Seq> will return a List that is still lazy, but stores the
12+
generated values for later access. Assigning a C<Seq> to an array consumes the C<Seq>;
13+
alternatively, you can use the C<lazy> statement prefix to avoid it from being
14+
iterated during the assignment:
1415
16+
=begin code
17+
# The Seq created by gather ... take is consumed on the spot here.
18+
my @a = gather do { say 'consuming...'; take 'one' }; # OUTPUT: «comsuming...␤»
19+
20+
# The Seq here is only consumed as we iterate over @a later.
21+
my @a = lazy gather do { say 'consuming...'; take 'one' }; # outputs nothing.
22+
.say for @a; # OUTPUT: «comsuming...␤one␤»
23+
=end code
24+
1525
A typical use case is L<method C<lines> in C<IO::Handle>|/type/IO::Handle#method_lines>,
1626
which could use a lot of memory if it stored all the lines read from the
1727
file. So

0 commit comments

Comments
 (0)