role Iterator
Generic API for producing a sequence of values
constant IterationEnd
A Iterator
is an object that can generate or provide elements of a sequence. Users usually don't have to care about iterators, their usage is hidden behind iteration APIs such as for @list { }
, map, grep, head, tail, skip and list indexing with .[$idx]
.
The main API is the pull-one
method, which either returns the next value, or the sentinel value IterationEnd
if no more elements are available. Each class implementing Iterator
must provide a pull-one
method. All other non-optional Iterator API methods are implemented in terms of pull-one
, but can also be overridden by consuming classes for performance or other reasons. There are also optional Iterator API methods that will only be called if they are implemented by the consuming class: these are not implemented by the Iterator role.
IterationEnd
Iterators only allow one iteration over the entire sequence. It's forbidden to make attempts to fetch more data, once IterationEnd
has been generated, and behavior for doing so is undefined. For example, the following Seq will not cause the die to be called under normal use, because pull-one will never be called after it returns IterationEnd
:
is Arraymy := SkippingArray.new;.append: 1, Any, 3, Int, 5, Mu, 7;for -> ,# OUTPUT: «[1 3][5 7]»
The only valid use of the sentinel value IterationEnd
in a program is identity comparison (using =:=
) with the result of a method in the iterator API. Any other behavior is undefined and implementation dependent.
Please bear in mind that IterationEnd
is a constant, so if you are going to compare it against the value of a variable, this variable will have to be bound, not assigned. Comparing directly to the output of pull-one
will work.
my = (1,2).iterator;.pull-one for ^2;say .pull-one =:= IterationEnd; # OUTPUT: «True»
However, if we use a variable we and we assign it, the result will be incorrect:
my = (1,2).iterator;.pull-one for ^2;my = .pull-one;say =:= IterationEnd; # OUTPUT: «False»
So we'll have to bind the variable to make it work:
my := .pull-one;say =:= IterationEnd; # OUTPUT: «True»
Methods
method pull-one
Defined as:
method pull-one(Iterator: --> Mu)
This method stub ensures that classes implementing the Iterator
role provide a method named pull-one
.
The pull-one
method is supposed to produce and return the next value if possible, or return the sentinel value IterationEnd
if no more values could be produced.
my = (1 .. 3).iterator;say .pull-one; # OUTPUT: «1»say .pull-one; # OUTPUT: «2»say .pull-one; # OUTPUT: «3»say .pull-one.perl; # OUTPUT: «IterationEnd»
As a more illustrative example of its use, here is a count down iterator along with a simplistic subroutine re-implementation of the for
loop.
# works the same as (10 ... 1, 'lift off')does Iteratorsub for( Iterable , --> Nil )for( Seq.new(CountDown.new), ); # OUTPUT: «10987654321lift off»
It would be more idiomatic to use while
or until
, and a sigilless variable.
until IterationEnd =:= (my \pulled = .pull-one)
method push-exactly
Defined as:
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.
method push-at-least
Defined as:
method push-at-least(Iterator: , int --> Mu)
Should produce at least $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
.
Iterators with side effects should produce exactly $count
elements; iterators without side effects (such as Range iterators) can produce more elements to achieve better performance.
my ;say (1 .. ∞).iterator.push-at-least(, 10); # OUTPUT: «10»say ; # OUTPUT: «[1 2 3 4 5 6 7 8 9 10]»
The Iterator role implements this method in terms of pull-one
. In general, it is also not intended to be called directly as in the example above. It can be implemented, if unhappy with this default implementation, by those using this role. See the documentation for push-exactly
for an example implementation.
method push-all
Defined as:
method push-all(Iterator: )
Should produce all elements from the iterator and push them to $target
.
my ;say (1 .. 1000).iterator.push-all(); # All 1000 values are pushed
The Iterator role implements this method in terms of push-at-least
. As in the case of the other push-*
methods, it is mainly intended for developers implementing this role. push-all
is called when assigning an object with this role to an array, for instance, like in this example:
does Iterable does Iterator;my := DNA.new("AAGCCT");my = ;say ; # OUTPUT: «[(A A G) (C C T)]»
The push-all
method implemented pushes to the target iterator in lists of three aminoacid representations; this is called under the covers when we assign $b
to @dna-array
.
method push-until-lazy
Defined as:
method push-until-lazy(Iterator: --> Mu)
Should produce values until it considers itself to be lazy, and push them onto $target
.
The Iterator role implements this method as a no-op if is-lazy
returns a True value, or as a synonym of push-all
if not.
This matters mostly for iterators that have other iterators embedded, some of which might be lazy, while others aren't.
method is-lazy
Defined as:
method is-lazy(Iterator: --> Bool)
Should return True
for iterators that consider themselves lazy, and False
otherwise.
Built-in operations that know that they can produce infinitely many values return True
here, for example (1..6).roll(*)
.
say (1 .. 100).is-lazy; # OUTPUT: «False»say (1 .. ∞).is-lazy; # OUTPUT: «True»
The Iterator role implements this method returning False
, indicating a non-lazy iterator.
method sink-all
Defined as:
method sink-all(Iterator: --> IterationEnd)
Should exhaust the iterator purely for the side-effects of producing the values, without actually saving them in any way. Should always return IterationEnd
. If there are no side-effects associated with producing a value, then it can be implemented by a consuming class to be a virtual no-op.
say (1 .. 1000).iterator.sink-all; # OUTPUT: «IterationEnd»
The Iterator role implements this method as a loop that calls pull-one
until it is exhausted.
method skip-one
Defined as:
method skip-one(Iterator: --> Mu)
Should skip producing one value. The return value should be truthy if the skip was successful and falsy if there were no values to be skipped:
my = <a b>.iterator;say .skip-one; say .pull-one; say .skip-one# OUTPUT: «1b0»
The Iterator role implements this method as a call pull-one
and returning whether the value obtained was not IterationEnd
.
method skip-at-least
Defined as:
method skip-at-least(Iterator: , int --> Mu)
Should skip producing $to-skip
values. The return value should be truthy if the skip was successful and falsy if there were not enough values to be skipped:
my = <a b c>.iterator;say .skip-at-least(2); say .pull-one; say .skip-at-least(20);# OUTPUT: «1c0»
The Iterator role implements this method as a loop calling skip-one
and returning whether it returned a truthy value sufficient number of times.
method skip-at-least-pull-one
Defined as:
method skip-at-least-pull-one(Iterator: , int --> Mu)
Should skip producing $to-skip
values and if the iterator is still not exhausted, produce and return the next value. Should return IterationEnd
if the iterator got exhausted at any point:
my = <a b c>.iterator;say .skip-at-least-pull-one(2);say .skip-at-least-pull-one(20) =:= IterationEnd;# OUTPUT: «cTrue»
The Iterator role implements this method as calling skip-at-least
and then calling pull-one
if it was not exhausted yet.
Predictive iterators
Please see the PredictiveIterator role if your Iterator
can know how many values it can still produce without actually producing them.