Documentation for routine flat
assembled from the following pages:
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 ((1, 2), (3), %(:42a)); # OUTPUT: «((1 2) 3 {a => 42})» say ((1, 2), (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 [[1, 2, 3], [(4, 5), 6, 7]] .flat; # OUTPUT: «([1 2 3] [(4 5) 6 7])» say [[1, 2, 3], [(4, 5), 6, 7]]».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: --> Supply)
Creates a supply on which all of the values seen in the given supply are flattened before being emitted again.
Role: Iterable §
From Iterable
(Iterable) method flat §
Defined as:
method flat(Iterable: --> 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: Backtrace §
From Backtrace
(Backtrace) method flat §
Defined as:
multi method flat(Backtrace:)
Returns the backtrace same as list.
Language documentation: Independent routines §
From Independent routines
(Independent routines) sub flat §
Defined as:
multi flat(**)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, (3, 4), $(5, 6)); # OUTPUT: «(1 2 3 4 (5 6))»
Class: Range §
From Range
(Range) method flat §
method flat(Range:)
Generates a Seq
containing the elements that the range represents.
Class: Array §
From Array
(Array) method flat §
Defined as:
multi method flat(Array:)multi method flat(Array:)
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 2 c>;say .flat.^name; # OUTPUT: «Seq»