# -*- mode: cperl6; -*- # 2011 Perl 6 Coding Contest # Edgar Gonzàlez i Pellicer # Linked lists module LinkedLists; # Linked list class LinkedList is export { has $.head; has LinkedList $.tail; # Elements method elems() { # Result my $result = 0; # Follow the list my LinkedList $l = self; # Next while $l.defined { # One more ++$result; # Continue $l .= tail; } # Return return $result; } # To array method to-array() { # Result my @result; # Follow the list my LinkedList $l = self; # Next while $l.defined { # Add the head @result.push($l.head); # Continue $l .= tail; } # Return return @result; } # Uniq -c method uniq-c() { # Result my @result; # Follow the list my LinkedList $l = self; # Next while $l.defined { # Get it my $start = $l.head; my $how-many = 1; # Continue $l .= tail; while $l.defined && $l.head == $start { $l .= tail; ++$how-many; } # Add @result.push($start => $how-many); } # Return return @result; } }