More Rosetta code written in Perl6

jhuni on 2010-03-19T11:21:11



I added a solution to the dot product, which it turns out was very easy knowing Perl6 meta operators:

[+] ( @a <<*>> @b )



Another problem I worked on is this missing permutations problem, which I solved using cross operators, yet it still doesn't feel that elegant to repeat things four times:

[X~] @letters = @letters X~ @letters X~ @letters X~ @letters

I would've used the [X~] operator, however, it does not seem to work in any implementation yet, so instead I wrote things like on the right hand side, which is quite messy. The only other thing which is sort of interesting is the code to check duplicates. I forget about the uniq function for a while so I wrote this messy code to check for duplicates:

sub lacksDuplicates($str) {

    my @chars = $str.split('');
    my @pastValues;

    for @chars -> $char {

        return ?(0) if $char eq any @pastValues;
        @pastValues.push($char);

    }

    return ?(1);

}


Fortunately we have uniq so I wrote something more pleasant instead:

.chars == .split('').uniq.elems