routine flat

Documentation for routine flat assembled from the following types:

class Any

From Any

(Any) method flat

Defined as:

method flat() is 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 the

hyper method call to call the .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

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.

language documentation Independent routines

From Independent routines

(Independent routines) sub flat

Defined as:

multi flat(**@list)
multi flat(Iterable \a)

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

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

class Range

From Range

(Range) method flat

method flat(Range:D:)

Generates a Seq containing the elements that the range represents.

role Iterable

From Iterable

(Iterable) method flat

Defined as:

method flat(Iterable:D: --> 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').raku;    # OUTPUT: «($("a", "b"), "c")␤»

You can use the hyper method call to call the .List method on all the inner itemized sublists and so de-containerize them, so that flat can flatten them:

say ($('a''b'), 'c')>>.List.flat.elems;    # OUTPUT: «3␤»

class Array

From Array

(Array) method flat

Defined as:

multi method flat(Array:U:)
multi method flat(Array:D:)

This method will return the type object itself if it's applied to a type object; when applied to an object, it will return a Seq created from the array's underlying iterator.

my @a = <a 2 c>;
say @a.flat.^name# OUTPUT: «Seq␤» 

class Backtrace

From Backtrace

(Backtrace) method flat

Defined as:

multi method flat(Backtrace:D:)

Returns the backtrace same as list.