@@ -13,17 +13,18 @@ sequence. Users usually don't have to care about iterators, their usage
13
13
is hidden behind iteration APIs such as C < for @list { } > , L < map > ,
14
14
L < grep > , L < head > , L < tail > , L < skip > and list indexing with C < .[$idx] > .
15
15
16
- The main API is the C < pull-one > method, which either returns the next value,
17
- or the sentinel value C < IterationEnd > if no more elements are available. Each
18
- class implementing C < Iterator > B < must > provide a C < pull-one > method. All other
19
- non-optional Iterator API methods are implemented in terms of C < pull-one > , but
20
- can also be overridden by consuming classes for performance or other reasons.
21
- There are also optional Iterator API methods that will only be called if they
22
- are implemented by the consuming class: these are B < not > implemented by the
16
+ The main API is the C < pull-one > method, which either returns the next
17
+ value, or the sentinel value C < IterationEnd > if no more elements are
18
+ available. Each class implementing C < Iterator > B < must > provide a
19
+ C < pull-one > method. All other non-optional Iterator API methods are
20
+ implemented in terms of C < pull-one > , but can also be overridden by
21
+ consuming classes for performance or other reasons. There are also
22
+ optional Iterator API methods that will only be called if they are
23
+ implemented by the consuming class: these are B < not > implemented by the
23
24
Iterator role.
24
25
25
- = head1 IterationEnd
26
26
X < |IterationEnd >
27
+ = head1 IterationEnd
27
28
28
29
Iterators only allow one iteration over the entire sequence. It's
29
30
forbidden to make attempts to fetch more data, once C < IterationEnd > has
@@ -58,9 +59,38 @@ for @a -> $a, $b {
58
59
# OUTPUT: «[1 3][5 8]»
59
60
= end code
60
61
61
- The only valid use of the sentinel value C < IterationEnd > in a program
62
- is identity comparison (using C < =:= > ) with the result of a method in the
63
- iterator API. Any other behavior is undefined and implementation dependent.
62
+ The only valid use of the sentinel value C < IterationEnd > in a program is
63
+ identity comparison (using C < =:= > ) with the result of a method in the
64
+ iterator API. Any other behavior is undefined and implementation
65
+ dependent.
66
+
67
+ Please bear in mind that C < IterationEnd > is a constant, so if you are
68
+ going to compare it against the value of a variable, this variable will
69
+ have to be bound, not assigned. Comparing directly to the output of
70
+ C < pull-one > will work.
71
+
72
+ = begin code
73
+ my $it = (1,2).iterator;
74
+ $it.pull-one for ^2;
75
+ say $it.pull-one =:= IterationEnd; # OUTPUT: «True»
76
+ = end code
77
+
78
+ However, if we use a variable we and we assign it, the result will be
79
+ incorrect:
80
+
81
+ = begin code
82
+ my $it = (1,2).iterator;
83
+ $it.pull-one for ^2;
84
+ my $is-it-the-end = $it.pull-one;
85
+ say $is-it-the-end =:= IterationEnd; # OUTPUT: «False»
86
+ = end code
87
+
88
+ So we'll have to bind the variable to make it work:
89
+
90
+ = begin code :preamble<my $it>
91
+ my $is-it-the-end := $it.pull-one;
92
+ say $is-it-the-end =:= IterationEnd; # OUTPUT: «True»
93
+ = end code
64
94
65
95
= head1 Methods
66
96
0 commit comments