I was wondering about how I could completely flatten lists and things that contain them. Among other things, I came up with this solution that slips things that have more than one element and puts them back, or takes things with one element after slipping it.
This is a bit different than How do I “flatten” a list of lists in perl 6?, which doesn't completely flat because the task is to restructure.
But, maybe there's a better way.
my @a = 'a', ('b', 'c' );
my @b = ('d',), 'e', 'f', @a;
my @c = 'x', $( 'y', 'z' ), 'w';
my @ab = @a, @b, @c;
say "ab: ", @ab;
my @f = @ab;
@f = gather {
while @f {
@f[0].elems == 1 ??
take @f.shift.Slip
!!
@f.unshift( @f.shift.Slip )
}
}
say "f: ", @f;
This gives:
ab: [[a (b c)] [(d) e f [a (b c)]] [x (y z) w]]
f: [a b c d e f a b c x y z w]
Curiously, I also read some python answers:
- Making a flat list out of list of lists in Python
- How flatten a list of lists one step
- flatten list of lists of lists to a list of lists
itertools.chain(*sublist)
look interesting, but the answers were either recursive or limited to two levels from hard-coding. The functional languages were recursive in the source code, but I expected that.
Unfortunately there's no direct built-in that completely flattens a data structure even when sub-lists are wrapped in item containers.
Some possible solutions:
Gather/take
You've already come up with a solution like this, but
deepmap
can take care of all the tree iteration logic to simplify it. Its callback is called once for every leaf node of the data structure, so usingtake
as the callback means thatgather
will collect a flat list of the leaf values:Custom recursive function
You could use a subroutine like this to recursively
slip
lists into their parent:Another approach would be to recursively apply
<>
to sub-lists to free them of any item containers they're wrapped in, and then callflat
on the result:Multi-dimensional array indexing
The
postcircumfix [ ]
operator can be used with a multi-dimensional subscript to get a flat list of leaf nodes up to a certain depth, though unfortunately the "infinite depth" version is not yet implemented:Still, if you know the maximum depth of your data structure this is a viable solution.
Avoiding containerization
The built-in
flat
function can flatten a deeply nested lists of lists just fine. The problem is just that it doesn't descend into item containers (Scalar
s). Common sources of unintentional item containers in nested lists are:An
Array
(but notList
) wraps each of its elements in a fresh item container, no matter if it had one before.:=
can be used instead of assignment, to store aList
in a@
variable without turning it into anArray
:$
variables are item containers.$
variable and then inserting it as an element into another list, use<>
to decontainerize it. The parent list's container can also be bypassed using|
when passing it toflat
:I'm unaware of a built-in way to do so, though there very well might be (and if not, there probably should be).
The best I could come up with on short notice is this:
I'm not sure how gather/take interacts with the potentially parallelized evaluation of hyper operators, so the following alternative might not be safe to use, in particular if you care about element order:
You can put the code into square brackets if you need an array or reify it into a list via
.list
.Lastly, this is the first solution rewitten as a retro-style subroutine: