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 non-containerized Iterables into a flat list, and returns that list. Keep in mind Map and Hash types are Iterable and so will be flattened into lists of pairs.

say ((12), (3), %(:42a));      # OUTPUT: «((1 2) 3 {a => 42})␤» 
say ((12), (3), %(:42a)).flat# OUTPUT: «(1 2 3 a => 42)␤» 

Note that Arrays containerize their elements by default, and so flat will not flatten them. You can use hyper method call to call .List method on all the inner Iterables and so de-containerize them, so that flat can flatten them:

say [[123], [(45), 67]]      .flat# OUTPUT: «([1 2 3] [(4 5) 6 7])␤» 
say [[123], [(45), 67]]».List.flat# OUTPUT: «(1 2 3 4 5 6 7)␤» 

For more fine-tuned options, see deepmap, duckmap, and signature destructuring

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.