P08 - 消除列表元素中的连续重复项。
作者:Eric Hodges
规范
P08 (**) Eliminate consecutive duplicates of list elements.
If a list contains repeated elements they should be replaced with a
single copy of the element. The order of the elements should not be
changed.示例
> say ~compress(<a a a a b c c a a d e e e e>) a b c a d e
源代码: P08-eric256.pl
use v6;
die "Example doesn't yet work in Niecza" if $*VM ~~ 'niecza';
sub compress (@in) {
my @return;
my $last;
for @in -> $i {
FIRST { $last = '' }
if ($i ne $last) {
@return.push($i);
$last = $i;
}
}
return @return;
}
compress(<a a a a b c c a a d e e e e>).perl.say;
Perl 6 示例