You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/Type/Iterator.pod6
+40-6Lines changed: 40 additions & 6 deletions
Original file line number
Diff line number
Diff line change
@@ -123,11 +123,11 @@ a simplistic subroutine re-implementation of the C<for> loop.
123
123
124
124
It would be more idiomatic to use C<while> or C<until>, and a sigilless variable.
125
125
126
-
=begincode :preamble<my $iterator = ().iterator; my &do = &say>
127
-
until IterationEnd =:= (my \pulled = $iterator.pull-one) {
128
-
do( pulled );
129
-
}
130
-
=endcode
126
+
=begincode :preamble<my $iterator = ().iterator; my &do = &say>
127
+
until IterationEnd =:= (my \pulled = $iterator.pull-one) {
128
+
do( pulled );
129
+
}
130
+
=endcode
131
131
132
132
=head2method push-exactly
133
133
@@ -146,7 +146,41 @@ C<$count>.
146
146
say (1 .. ∞).iterator.push-exactly(@array, 3); # OUTPUT: «3»
147
147
say @array; # OUTPUT: «[1 2 3]»
148
148
149
-
The Iterator role implements this method in terms of C<pull-one>.
149
+
The Iterator role implements this method in terms of C<pull-one>. In general,
150
+
this is a method that is not intended to be called directly from the end user
151
+
who, instead, should implement it in classes that mix the iterator role. For
152
+
instance, this class implements that role:
153
+
154
+
=begincode
155
+
class DNA does Iterable does Iterator {
156
+
has $.chain;
157
+
has Int $!index = 0;
158
+
159
+
method new ($chain where {
160
+
$chain ~~ /^^ <[ACGT]>+ $$ / and
161
+
$chain.chars %% 3 } ) {
162
+
self.bless( :$chain );
163
+
}
164
+
165
+
method push-exactly(Iterator:D: $target, int $count --> Mu) {
166
+
return IterationEnd if $.chain.elems / 3 < $count;
167
+
for ^($count) {
168
+
$target.push: $.chain.comb.rotor(3)[ $_ ];
169
+
}
170
+
}
171
+
172
+
};
173
+
174
+
my $b := DNA.new("AAGCCT");
175
+
for $b -> $a, $b, $c { say "Never mind" }; # Does not enter the loop
176
+
my $þor := DNA.new("CAGCGGAAGCCT");
177
+
for $þor -> $first, $second {
178
+
say "Coupled codons: $first, $second";
179
+
# OUTPUT: «Coupled codons: C A G, C G GCoupled codons: A A G, C C T»
180
+
}
181
+
=endcode
182
+
183
+
This code, which groups DNA chains in triplets (usually called I<codons>) returns those codons when requested in a loop; if too many are requested, like in the first case C«for $b -> $a, $b, $c», it simply does not enter the loop since C<push-exactly> will return C<IterationEnd> since it is not able to serve the request for exactly 3 codons. In the second case, however, it requests exactly two codons in each iteration of the loop; C<push-exactly> is being called with the number of loop variables as the C<$count> variable.
0 commit comments