Skip to content

Commit 21855f4

Browse files
committed
Initial Seq documentation
1 parent cbb84c8 commit 21855f4

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

lib/Type/Seq.pod

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
=begin pod
2+
3+
=TITLE class Seq
4+
5+
=SUBTITLE An iterable, lazy sequence of values
6+
7+
class Seq is Cool does Iterable does PositionalBindFailover { }
8+
9+
A C<Seq> represents anything that can lazily produce a sequence of values. A
10+
C<Seq> is born in a state where iterating it will consume the values. However,
11+
calling .list 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.
14+
15+
A typical use case is L<method C<lines> in C<IO::Handles>|/type/IO::Handles#method lines>,
16+
which could use quite much memory if it stored all the lines read from the
17+
file. So
18+
19+
for open('README').lines -> $line {
20+
say $line;
21+
}
22+
23+
won't keep all lines from the file in memory.
24+
25+
This implies that you cannot iterate the same C<Seq> object twice (otherwise
26+
it couldn't throw away old values), so this dies:
27+
28+
=begin code :allow<L>
29+
my \lines := open('README').lines;
30+
for lines { .say };
31+
for lines { .say }; # dies with "This Seq has already been iterated [...]"
32+
# of type L<X::Seq::Consumed>
33+
=end code
34+
35+
A high-level construct to generate a C<Seq> is C<gather/take>, as well as many
36+
built-in methods like C<map> and C<grep>, low-level constructors to create a
37+
Seq from an iterator or from looping constrcuts are available too.
38+
39+
=head1 Methods
40+
41+
=end pod

0 commit comments

Comments
 (0)