See Original text in context
method push-exactly(Iterator: , int --> Mu)
Should produce $count
elements, and for each of them, call $target.push($value)
.
If fewer than $count
elements are available from the iterator, it should return the sentinel value IterationEnd
. Otherwise it should return $count
.
my ;say (1 .. ∞).iterator.push-exactly(, 3); # OUTPUT: «3»say ; # OUTPUT: «[1 2 3]»
The Iterator role implements this method in terms of pull-one
. In general, this is a method that is not intended to be called directly from the end user who, instead, should implement it in classes that mix the iterator role. For instance, this class implements that role:
does Iterable does Iterator;my := DNA.new("AAGCCT");for -> , , ; # Does not enter the loopmy := DNA.new("CAGCGGAAGCCT");for -> ,
This code, which groups DNA chains in triplets (usually called codons) returns those codons when requested in a loop; if too many are requested, like in the first case for $b -> $a, $b, $c
, it simply does not enter the loop since push-exactly
will return 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; push-exactly
is being called with the number of loop variables as the $count
variable.