P08 - Eliminate consecutive duplicates of list elements.

Author: Ryan Connelly

Example

> say eliminate-consecutive-duplicates(<a a a a a b b c b d e e>);
a b c b d e

Source code: P08-topo.pl

use v6;

sub eliminate-consecutive-duplicates(@list)
{
    my $last = ();

    gather for @list -> $e
    {
        # ~~ is the smart match operator.
        # It's the closest thing I can think of to a comparison function
        # which doesn't enforce a particular context... should be alright. :-)
        next if $e ~~ $last;

        $last = $e;

        take $e;
    }
}

say "{eliminate-consecutive-duplicates(<a a a a a b b c b d e e>)}";