Skip to content

Commit

Permalink
Improves a bit the documentation on Iterator/Iterable
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed Jul 20, 2019
1 parent 6cd0b18 commit 9b2d884
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions doc/Language/iterating.pod6
Expand Up @@ -38,22 +38,22 @@ say @longer-chain».join("").join("|"); #OUTPUT: «ACG|TAC|GTT␤»
=end code
In this example, which is an extension of the L<example in C<Iterable> that
shows how C<for> calls C<.iterator>|/type/Iterable>, this method is called
simply when the created object is assigned to a L<Positional|/type/Positional> variable,
C<@longer-chain>; this variable is an L<Array|/type/Array> and we operate on that in the
last example.
shows how C<for> calls C<.iterator>|/type/Iterable>, the C<iterator> method will
be called in the appropriate context only when the created object is assigned to
a L<Positional|/type/Positional> variable, C<@longer-chain>; this variable is an
L<Array|/type/Array> and we operate on it as such in the last example.
The (maybe a bit confusingly named) C<Iterator> role is a bit more complex than
C<Iterable>. First, it provides a constant, C<IterationEnd>, but then it
C<Iterable>. First, it provides a constant, C<IterationEnd>. Then, it also
provides a series of L<methods|/type/Iterator#Methods> such as C<.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 C<pull-one>, of which only a stub is
provided. While C<Iterable> provides the high-level variable loops will be
working in, C<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.
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 C<pull-one>, of which only a stub is
provided by the role. While C<Iterable> provides the high-level interface loops
will be working with, C<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.
=begin code
class DNA does Iterable does Iterator {
Expand Down Expand Up @@ -84,9 +84,11 @@ my $a := DNA.new('GAATCC');
We declare a C<DNA> class which does the two roles, C<Iterator> and C<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
is a multiple of 3 and composed only of ACGT.
Let us look at the
C<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 C<$.index> property will
occurs, so it must keep the state of the last one. A C<$.index> atrribute will
hold that state across invocations; C<pull-one> will check if the end of the
chain has been reached and will return the C<IterationEnd> constant provided by
the role. Implementing this low-level interface, in fact, simplifies the
Expand All @@ -96,9 +98,10 @@ C<.iterator> will thus return just C<self>; this is possible since the object
will be, at the same time, C<Iterable> and C<Iterator>.
This need not always be the case, and in most cases C<.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 iterator and iterable roles.
build an iterator type to be returned (that will, for instance, keep track of
the iteration state, which we are doing now in the main class), such as we did
in the previous example; however, this example shows the minimal code needed to
build a class that fulfills the iterator and iterable roles.
=head1 How to iterate: contextualizing and topic variables
Expand Down
File renamed without changes.

0 comments on commit 9b2d884

Please sign in to comment.