role Iterable

Interface for container objects that can be iterated over

role Iterable { }

Iterable serves as an API for objects that can be iterated with the for construct and related iteration constructs, like hyper operators.

Iterable objects nested in other Iterable objects (but not within scalar containers) flatten in certain contexts, for example when passed to a slurpy parameter (*@a), or on explicit calls to flat.

Its important aspect is a method stub for iterator.

role DNA does Iterable {
    method iterator(){ self.comb.iterator }
my @a does DNA = 'GAATCC';
.say for @a# OUTPUT: «G␤A␤A␤T␤C␤C␤» 


method iterator

Defined as:

method iterator(--> Iterator:D)

Method stub that ensures all classes doing the Iterable role have a method iterator.

It is supposed to return an Iterator.

say (1..10).iterator;

method flat

Defined as:

method flat(--> Iterable)

Returns another Iterable that flattens out all iterables that the first one returns.

For example

say (<a b>'c').elems;         # OUTPUT: «2␤» 
say (<a b>'c').flat.elems;    # OUTPUT: «3␤» 

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:

say ($('a''b'), 'c').perl;    # OUTPUT: «($("a", "b"), "c")␤» 

method lazy

Defined as:

method lazy(--> Iterable)

Returns a lazy iterable wrapping the invocant.

say (1 ... 1000).is-lazy;      # OUTPUT: «False␤» 
say (1 ... 1000) OUTPUT: «True␤» 

method hyper

Defined as:

method hyper(Int(Cool:$batch = 64Int(Cool:$degree = 4 --> Iterable)

More information

Returns another Iterable that is potentially iterated in parallel, with a given batch size and degree of parallelism.

The order of elements is preserved.

say ([1..100]{ $_ +1 }).list);

method race

Defined as:

method race(Int(Cool:$batch = 64Int(Cool:$degree = 4 --> Iterable)

More information

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.

say ([1..100]{ $_ +1 }).list);

Type Graph

Type relations for Iterable
perl6-type-graph Iterable Iterable Mu Mu Any Any Any->Mu Cool Cool Cool->Any Positional Positional Range Range Range->Iterable Range->Cool Range->Positional List List List->Iterable List->Cool List->Positional PositionalBindFailover PositionalBindFailover Seq Seq Seq->Iterable Seq->Cool Seq->PositionalBindFailover Associative Associative Map Map Map->Iterable Map->Cool Map->Associative Slip Slip Slip->List Array Array Array->List PseudoStash PseudoStash PseudoStash->Map Hash Hash Hash->Map Stash Stash Stash->Hash

Stand-alone image: vector