Iterable Reference
Interface for container objects that can be iterated over
What it is #
Position in the hierarchy #
Routines #
Signatures are reproduced from the documentation, including their declared return types. A routine listed here is part of the type's published interface; whether Raku++ implements it is a separate question, answered by the examples below.
method iterator #
method iterator(--> Iterator:D)
Returns Iterator:D.
Method stub that ensures all classes doing the Iterable role have a method iterator. It is supposed to return an Iterator.
method flat #
method flat(Iterable:D: --> Iterable)
Returns Iterable.
Returns another Iterable that flattens out all iterables that the first one returns. For example because <a b> is a List and thus iterable, so (<a b>, 'c').flat returns ('a', 'b', 'c'), which has three elems. Note that the flattening is recursive, so ((("a", "b"), "c"), "d").flat returns ("a", "b", "c", "d"), but it does not flatten itemized sublists:
method lazy #
method lazy(--> Iterable)
Returns Iterable.
Returns a lazy iterable wrapping the invocant.
method hyper #
method hyper(Int(Cool) :$batch = 64, Int(Cool) :$degree = Kernel.cpu-cores - 1)
Returns another Iterable that is potentially iterated in parallel, with a given batch size and degree of parallelism. The order of elements is preserved. Use hyper in situations where it is OK to do the processing of items in parallel, and the output order should be kept relative to the input order. See race for situations where items are processed in parallel and the output order does not matter.
method race #
method race(Int(Cool) :$batch = 64, Int(Cool) :$degree = 4 --> Iterable)
Returns Iterable.
Returns another Iterable that is potentially iterated in parallel, with a given batch size and degree of parallelism (number of parallel workers). Unlike hyper, race does not preserve the order of elements (mnemonic: in a race, you never know who will arrive first). Use race in situations where it is OK to do the processing of items in parallel, and the output order does not matter. See hyper for
Examples, run three ways #
Every example below comes from the official documentation, together with the output that documentation asserts. Each was then executed by Rakudo and by Raku++ when this page was built. Where the three agree, one result is shown; where they do not, all three are — because which of them is wrong is exactly the information worth having.
1 doc-drift · 4 no-output · 4 ok
role Iterable { }Not executed: the documentation states no expected output for this example.
class DNA does Iterable {
has $.chain;
method new ($chain where { $chain ~~ /^^ <[ACGT]>+ $$ / } ) {
self.bless( :$chain );
}
method iterator(DNA:D:) {
$!chain.comb.rotor(3).iterator;
}
}
my $a := DNA.new('GAATCC');
.say for $a; # OUTPUT: «(G A A)(T C C)»(G A A)
(T C C)
Documentation, Rakudo and Raku++ all agree.
say (1..10).iterator;
Not executed: the documentation states no expected output for this example.
say (<a b>, 'c').elems; # OUTPUT: «2» say (<a b>, 'c').flat.elems; # OUTPUT: «3»
2
3
Documentation, Rakudo and Raku++ all agree.
say ($('a', 'b'), 'c').flat; # OUTPUT: «($("a", "b"), "c")»($("a", "b"), "c")
((a b) c)
((a b) c)
Both engines agree; the documentation states something else. Trust the engines.
say ($('a', 'b'), 'c')>>.List.flat.elems; # OUTPUT: «3»3
Documentation, Rakudo and Raku++ all agree.
say (1 ... 1000).is-lazy; # OUTPUT: «False» say (1 ... 1000).lazy.is-lazy; # OUTPUT: «True»
False
True
Documentation, Rakudo and Raku++ all agree.
say ([1..100].hyper.map({ $_ +1 }).list);Not executed: the documentation states no expected output for this example.
say ([1..100].race.map({ $_ +1 }).list);Not executed: the documentation states no expected output for this example.