title: How can I completely flatten a list (of lists (of lists) ... )
body: <p>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.</p>
  
  <p>This is a bit different than <a href="https://stackoverflow.com/q/37173023/2766176">How do I “flatten” a list of lists in perl 6?</a>, which doesn't completely flat because the task is to restructure. </p>
  
  <p>But, maybe there's a better way.</p>
  
  <pre><code>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;
  </code></pre>
  
  <p>This gives:</p>
  
  <pre><code>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]
  </code></pre>
  
  <p>Curiously, I also read some python answers:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/q/952914/2766176">Making a flat list out of list of lists in Python</a></li>
  <li><a href="https://stackoverflow.com/q/1688712/2766176">How flatten a list of lists one step</a></li>
  <li><a href="https://stackoverflow.com/q/33355299/2766176">flatten list of lists of lists to a list of lists</a></li>
  </ul>
  
  <p><code>itertools.chain(*sublist)</code> 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.</p>