File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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
You can’t perform that action at this time.
0 commit comments