P17 - Split a list into two parts; the length of the first part is given.

Author: Ryan Connelly

Example

> say split-list(('a' xx 10).list, 8).perl;
(["a", "a", "a", "a", "a", "a", "a", "a"], ["a", "a"]).list

Source code: P17-topo.pl

use v6;

sub split-list(@list, $length) {
    my $i = 0;

    gather while $i <= $length {
        take [ gather while $i <= $length {
            $i++ and take @list.shift;
        } ];

        take [ @list ];
    }
}

say split-list(['a' xx 20], 8).list.perl;