In role Iterable§
See primary documentation in context for method flat
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').flat; # 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»
In Array§
See primary documentation in context for method flat
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
underlying iterator.
my @a = <a 2 c>; say @a.flat.^name; # OUTPUT: «Seq»
In Backtrace§
See primary documentation in context for method flat
multi method flat(Backtrace:D:)
Returns the backtrace same as list.
In Any§
See primary documentation in context for method flat
method flat() is nodal
Interprets the invocant as a list, flattens non-containerized Iterable
s 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 Array
s 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 Iterable
s 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
In Range§
See primary documentation in context for method flat
method flat(Range:D:)
Generates a Seq
containing the elements that the range represents.
In Supply§
See primary documentation in context for 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.
In Independent routines§
See primary documentation in context for sub flat
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, (3, 4), $(5, 6)); # OUTPUT: «(1 2 3 4 (5 6))»