Skip to content

Commit d6698c4

Browse files
committed
Includes better examples for .push methods in Iterator docs.
Which then closes #1395
1 parent 4a5c966 commit d6698c4

File tree

1 file changed

+55
-1
lines changed

1 file changed

+55
-1
lines changed

doc/Type/Iterator.pod6

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,18 @@ class DNA does Iterable does Iterator {
162162
self.bless( :$chain );
163163
}
164164
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+
165177
method push-exactly(Iterator:D: $target, int $count --> Mu) {
166178
return IterationEnd if $.chain.elems / 3 < $count;
167179
for ^($count) {
@@ -227,7 +239,49 @@ Should produce all elements from the iterator and push them to C<$target>.
227239
my @array;
228240
say (1 .. 1000).iterator.push-all(@array); # All 1000 values are pushed
229241
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>.
231285
232286
=head2 method push-until-lazy
233287

0 commit comments

Comments
 (0)