Documentation for routine flat

Documentation for routine flat, assembled from the following types:

class Any

From Any

(Any) method flat

Defined as:

method flat(--> Seq:Dis nodal

Interprets the invocant as a list, flattens it, and returns that list. Please note that .flat will not solve the halting problem for you. If you flat an infinite list .flat may return that infinite list, eating all your RAM in the process.

say ((12), (3)).elems;        # OUTPUT: «2␤» 
say ((12), (3)).flat.elems;   # OUTPUT: «3␤» 

Please note that flat does not recurse into sub lists. You have to recurse by hand or reconsider your data structures. A single level of nesting can often be handled with destructuring in signatures. For deeper structures you may consider gather/take to create a lazy list.

my @a = [[1,2,3],[[4,5],6,7]];
say gather deepmap *.take@a# OUTPUT: «(1 2 3 4 5 6 7)␤» 

role Iterable

From Iterable

(Iterable) 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")␤» 

class List

From List

(List) sub flat

Defined as:

sub flat(**@list is raw)

Constructs a list which contains any arguments provided in the order provided, and returns the result of calling the .flat method (inherited from Any) on that list:

say flat 1, (2, (34), $(56)); # OUTPUT: «(1 2 3 4 (5 6))␤» 

class Range

From Range

(Range) method flat

method flat(Range:D: --> List:D)

Generates the list of elements that the range represents.

class Supply

From Supply

(Supply) method flat

method flat(Supply:D: --> Supply:D)

Creates a supply on which all of the values seen in the given supply are flattened before being emitted again.