P17 - 将一个列表分成两部分;第一部分的长度是给定的。
作者:Ryan Connelly
示例
> say split-list(('a' xx 10).list, 8).perl;
(["a", "a", "a", "a", "a", "a", "a", "a"], ["a", "a"]).list源代码: 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;
Perl 6 示例