Examples collection
/ P08 - Eliminate consecutive duplicates of list elements.
P08 - Eliminate consecutive duplicates of list elements.
We define an infix operator called 'compress' such that:
'a' compress 'a' gives 'a'
'a' compress 'b' gives ('a','b')
(@a,'a') compress 'a' gives ( @a,'a')
(@a,'a') compress 'b' gives ( @a,'a','b')
Now all we need to do is split our array up and insert compress.
given <a a b c c d> we want:
'a' compress 'a' compress 'b' compress 'c' compress 'c' compress 'd'
The reduce metaoperator does exactly this. For example:
[+] (1,2,3,4,5) == 1 + 2 + 3 + 4 + 5 == 15
[~] <a b c d e> eq 'a' ~ 'b' ~ 'c' ~ 'd' ~ 'e' eq 'abcde'
Specification
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.
Example
> say [compress] <a a a a b c c a a d e e e e>
a b c a d e
use v6;
multi infix:<compress> ( $a, $b ) { $a ~~ $b ?? $a !! ( $a, $b ) }
multi infix:<compress> ( @a, $b ) { @a[*-1] ~~ $b ?? @a !! ( @a, $b ).flat }
say ([compress] <a a a a b c c a a d e e e e>).perl;
# vim: expandtab shiftwidth=4 ft=perl6
See Also P04-topo.pl
P04 - Find the number of elements in a list.
P06-ajs.pl
P06 - Find out whether a list is a palindrome.
P06-topo.pl
P06 - Find out whether a list is a palindrome.
P08-eric256.pl
P08 - Eliminate consecutive duplicates of list elements.
P08-topo.pl
P08 - Eliminate consecutive duplicates of list elements.
P09-rje.pl
P09 - Pack consecutive duplicates of list elements into sublists.
P09-scottp.pl
P09 - Pack consecutive duplicates of list elements into sublists.
P09-topo.pl
P09 - Pack consecutive duplicate elements of a list into sublists.
P09-unobe.pl
P09 - Pack consecutive duplicates of list elements into sublists.
P13-rhebus.pl
P13 - Run-length encoding of a list (direct solution).
P15-rhebus.pl
P15 - Replicate the elements of a list a given number of times.
P15-topo.pl
P15 - Replicate the elements of a list a given number of times.
P15-unobe.pl
P15 - Replicate the elements of a list a given number of times.
P17-topo.pl
P17 - Split a list into two parts; the length of the first part is given.
P17-unobe.pl
P17 - Split a list into two parts; the length of the first part is given.
P18-topo.pl
P18 - Extract a slice from a list. Indices start at 1.
P21-scottp.pl
P21 - Insert an element at a given position into an array.
P21-topo.pl
P21 - Insert an element at a given position into a list.
P22-scottp.pl
P22 - Create a list containing all integers within a given range.
P22-topo.pl
P22 - Create a list containing all integers within a given range.
P23-topo.pl
P23 - Extract a given number of randomly selected elements from a list.
P24-topo.pl
P24 - Draw N different random numbers from the set 1..M.
P25-topo.pl
P25 - Generate a random permutation of the elements of a list.
P26-topo.pl
P26 - Generate the combinations of k distinct objects chosen from the n elements of a list.
P31-rhebus.pl
P31 - Determine whether a given integer number is prime.
P32-rhebus.pl
P32 - Determine the greatest common divisor of two positive integer
P33-rhebus.pl
P33 - Determine whether two positive integer numbers are coprime.
P35-rhebus.pl
P35 - Determine the prime factors of a given positive integer.
P36-ovid.pl
P36 - Determine the prime factors of a given positive integer (2).
P36-rhebus.pl
P36 - Determine the prime factors of a given positive integer (2).
P37-rhebus.pl
P37 - Calculate Euler's totient function phi(m) (improved).
š¦ / Examples collection
/ P08 - Eliminate consecutive duplicates of list elements.
The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society.
All rights reserved.