@@ -162,6 +162,18 @@ class DNA does Iterable does Iterator {
162
162
self.bless( :$chain );
163
163
}
164
164
165
+ method iterator( ){ self }
166
+
167
+ method pull-one( --> Mu){
168
+ if $!index < $.chain.chars {
169
+ my $codon = $.chain.comb.rotor(3)[$!index div 3];
170
+ $!index += 3;
171
+ return $codon;
172
+ } else {
173
+ return IterationEnd;
174
+ }
175
+ }
176
+
165
177
method push-exactly(Iterator:D: $target, int $count --> Mu) {
166
178
return IterationEnd if $.chain.elems / 3 < $count;
167
179
for ^($count) {
@@ -227,7 +239,49 @@ Should produce all elements from the iterator and push them to C<$target>.
227
239
my @array;
228
240
say (1 .. 1000).iterator.push-all(@array); # All 1000 values are pushed
229
241
230
- The Iterator role implements this method in terms of C < push-at-least > .
242
+ The Iterator role implements this method in terms of C < push-at-least > . As in the
243
+ case of the other C < push-* > methods, it is mainly intended for developers
244
+ implementing this role. C < push-all > is called when assigning an object with this
245
+ role to an array, for instance, like in this example:
246
+
247
+ = begin code
248
+ class DNA does Iterable does Iterator {
249
+ has $.chain;
250
+ has Int $!index = 0;
251
+
252
+ method new ($chain where {
253
+ $chain ~~ /^^ <[ACGT]>+ $$ / and
254
+ $chain.chars %% 3 } ) {
255
+ self.bless( :$chain );
256
+ }
257
+
258
+ method iterator( ){ self }
259
+ method pull-one( --> Mu){
260
+ if $!index < $.chain.chars {
261
+ my $codon = $.chain.comb.rotor(3)[$!index div 3];
262
+ $!index += 3;
263
+ return $codon;
264
+ } else {
265
+ return IterationEnd;
266
+ }
267
+ }
268
+
269
+ method push-all(Iterator:D: $target) {
270
+ for $.chain.comb.rotor(3) -> $codon {
271
+ $target.push: $codon;
272
+ }
273
+ }
274
+
275
+ };
276
+
277
+ my $b := DNA.new("AAGCCT");
278
+ my @dna-array = $b;
279
+ say @dna-array; # OUTPUT: «[(A A G) (C C T)]»
280
+ = end code
281
+
282
+ The C < push-all > method implemented pushes to the target iterator in lists of
283
+ three aminoacid representations; this is called under the covers when we assign
284
+ C < $b > to C < @dna-array > .
231
285
232
286
= head2 method push-until-lazy
233
287
0 commit comments