Iterators: Difference between revisions

Content added Content deleted
(→‎{{header|Raku}}: Add a Raku example)
m (→‎{{header|Raku}}: abstract away some intermediate variables to not clutter up the example)
Line 138: Line 138:
Rakus iterators are one direction only (not reversible), and once the iteration has been reified, it is discarded unless explicitly cached. This allows effortless iteration through multi-gigabyte sized data objects and streams without filling up main memory.
Rakus iterators are one direction only (not reversible), and once the iteration has been reified, it is discarded unless explicitly cached. This allows effortless iteration through multi-gigabyte sized data objects and streams without filling up main memory.


The following example iterates though a hash of Positional Iterable objects and demonstrates object slice operations on each; then has a semi contrived example of where directly using iterators may be actually useful in Raku.
The following example iterates though a hash of Positional Iterable objects and demonstrates object slice operations on each; then has a semi contrived example of where directly using iterators may be actually useful in Raku; collating unique ascending values from several infinite sequence generators.


<lang perl6>my %positional-iterable-types =
<lang perl6>my %positional-iterable-types =
Line 159: Line 159:


say "\nWhere iterators really shine; when you are trying to collate the values from several infinite generators.";
say "\nWhere iterators really shine; when you are trying to collate the values from several infinite generators.";
my @a = 1, * × 2 … *;
my @i = (1, * × 2 … *).iterator, (1, * × 3 … *).iterator, (1, * × 5 … *).iterator;
my @b = 1, * × 3 … *;
my @c = 1, * × 5 … *;

my @i = [@a.iterator, @b.iterator, @c.iterator];
my @v = @i[0].pull-one, @i[1].pull-one, @i[2].pull-one;
my @v = @i[0].pull-one, @i[1].pull-one, @i[2].pull-one;