4
> my @a = (3, [4,5, [6, 7 , (8,9, (10), 11), 12], 13], 14, 15)
[3 [4 5 [6 7 (8 9 10 11) 12] 13] 14 15]
> flat @a
(3 [4 5 [6 7 (8 9 10 11) 12] 13] 14 15) # ---------------------------> not flat enough
> say @a[*;*;*]
(3 4 5 6 7 (8 9 10 11) 12 13 14 15) # -------------------------------> but I don't know in advance how deep

my @a = (3, [4,5, [6, 7 , (8,9, (10), 11), 12], 13], 14, 15, ( "a", "A", "b", "B"), 99);
my @result = ();
sub flatDeep( $x ) {
    if $x ~~ (List, Array).any { # -----------------------------> not yet working on Hash or Set
        if $x.elems > 0 {
            flatDeep( $x[0] ) ;
            flatDeep( $x[1..*] );
        } 
    } else {
        @result.push( $x );
    };
}
flatDeep( @a );
say @result;

[3 4 5 6 7 8 9 10 11 12 13 14 15 a A b B 99] # ----------------------> achieved by clumsy routine not yet working on Hash or Set;

Any other simple and elegant way to do the same? Thanks.

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
lisprogtor
  • 5,677
  • 11
  • 17

0 Answers0