Skip to content

Commit

Permalink
Changes Iterable example, closes #2074
Browse files Browse the repository at this point in the history
  • Loading branch information
JJ committed May 29, 2018
1 parent f565592 commit 264b941
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions doc/Type/Iterable.pod6
Expand Up @@ -13,14 +13,27 @@ C<Iterable> objects nested in other C<Iterable> objects (but not within scalar
containers) flatten in certain contexts, for example when passed to a slurpy
parameter (C<*@a>), or on explicit calls to C<flat>.
Its important aspect is a method stub for C<iterator>.
Its most important aspect is a method stub for C<iterator>.
role DNA does Iterable {
method iterator(){ self.comb.iterator }
};
=begin code
class DNA does Iterable {
has $.chain;
method new ($chain where { $chain ~~ /^^ <[ACGT]>+ $$ / } ) {
self.bless( :$chain );
}
my @a does DNA = 'GAATCC';
.say for @a; # OUTPUT: «G␤A␤A␤T␤C␤C␤»
method iterator(DNA:D:){ $.chain.comb.rotor(3).iterator }
};
my $a := DNA.new('GAATCC');
.say for $a; # OUTPUT: «(G A A)␤(T C C)␤»
=end code
This example mixes in the Iterable role to offer a new way of iterating over
what is essentially a string (constrained by
L<C<where>|/type/Signature#index-entry-where_clause_(Signature)> to just the
four DNA letters). In the last statement, C<for> actually hooks to the C<iterator> role printing the letters in groups of 3.
=head1 Methods
Expand Down

0 comments on commit 264b941

Please sign in to comment.