Functionalities available for visiting all items in a complex data structure
Perl 6 is a functional language, but functions need something to hold on to when working on complex data structures. In particular, they need an uniform interface that can be applied to all of them. This interface is provided by the Iterator and Iterable roles.
Iterable role is relatively simple. It provides a stub for the
iterator method, which is the one actually used by statements such as
for will call
.iterator on the variable it precedes, and then run a block once for every item. Other methods, such as array assignment, will make the
Iterable class behave in the same way.
does Iterable;my = DNA.new('ACGTACGTT');say .perl;# OUTPUT: «[("A", "C", "G"), ("T", "A", "C"), ("G", "T", "T")]␤»say ».join("").join("|"); #OUTPUT: «ACG|TAC|GTT␤»
In this example, which is an extension of the example in
Iterable that shows how
.iterator, this method is called simply when the created object is assigned to a Positional variable,
@longer-chain; this variable is an Array and we operate on that in the last example.
The (maybe a bit confusingly named)
Iterator role is a bit more complex than
Iterable. First, it provides a constant,
IterationEnd, but then it provides a series of methods such as
.pull-one, which allows for a finer operation of iteration in several contexts: adding or eliminating items, or skipping over them to access other items. In fact, the role provides a default implementation for all the other methods, so the only one that has to be defined is precisely
pull-one, of which only a stub is provided. While
Iterable provides the high-level variable loops will be working in,
Iterator provides the lower-level functions that will be called in every iteration of the loop. Let's extend the previous example with this role.
does Iterable does Iterator;my := DNA.new('GAATCC');.say for ; # OUTPUT: «(G A A)␤(T C C)␤»
We declare a
DNA class which does the two roles,
Iterable; the class will include a string that will be constrained to have a length that is a multiple of 3 and composed only of ACGT. Let us first look at the
pull-one method. This one is going to be called every time a new iteration occurs, so it must keep the state of the last one. A
$.index property will hold that state across invocations;
pull-one will check if the end of the chain has been reached and will return the
IterationEnd constant provided by the role. Implementing this low-level interface, in fact, simplifies the implementation of the
Iterable interface. Now the iterator will be the object itself, since we can call
pull-one on it to access every member in turn;
.iterator will thus return just
self; this is possible since the object will be, at the same time,
This need not always be the case, and in most cases
.iterator will have to build an iterator type to be returned, such as we did in the previous example; however, this example shows the minimal code needed to build a class that fulfills the two iter(ator|able) roles.
for and other loops place the item produced in every iteration into the topic variable
$_, or capture them into the variables that are declared along with the block. These variables can be directly used inside the loop, without needing to declare them, by using the
Implicit iteration occurs when using the sequence operator.
say 1,1,1, … * > 300; # OUTPUT: «(1 1 1 4 7 16 46 127 475)
The generating block is being run once while the condition to finish the sequence, in this case the term being bigger than 300, is not met. This has the side effect of running a loop, but also creating a list that is output.
This can be done more systematically through the use of the
gather/take blocks, which are a different kind of iterating construct that instead of running in sink context, returns an item every iteration. This Advent Calendar tutorial explains use cases for this kind of loops; in fact, gather is not so much a looping construct, but a statement prefix that collects the items produced by
take and creates a list out of them.
However, in general, they are discouraged. Perl 6 is a functional and concurrent language; when coding in Perl 6, you should look at loops in a functional way: processing, one by one, the items produced by an iterator, that is, feeding an item to a block without any kind of secondary effects. This functional view allows also easy parallelization of the operation via the
race auto-threading methods.
If you feel more comfortable with your good old loops, the language allows you to use them. However, it is considered more p6y to try and use, whenever possible, functional and concurrent iterating constructs.