StÃphane Payrard writes:
> I use over and over this idiom in perl5:
>
> $a{$_}++ for @a;
>
> This is nice and perlish but it gets easily pretty boring
> when dealing with many list/arrays and counting hashes.
>
> I thought overloading the += operator
>
> %a += @a;
Though that would like to mean that:
%a + @a
Is %a with each of @a incremented. Which is quite nonsense, since it's
the number of keys of %a plus the number of values of @a.
Obviously adding to a hash means nothing, so there's no ambiguity with
using +=, but it's that disagreement that makes me uneasy.
But if you must, I believe it's possible:
multi sub *infix:+= (%hash is rw, $scalar) is rw {
++%hash{$scalar}; %hash;
}
multi sub *infix:+= (%hash is rw, @array) is rw {
++Â [EMAIL PROTECTED]; %hash;
}
[snip]
> Having real types in Perl6 will allow to slice, dice, splice data
> in many nice ways. Damian can even spice that with junctions.
> Fear. Fear.
Yeah, junctions fit the purpose better, unless you're histogramming.
Or better yet, since Junctions can only be in scalar variables, one
might use | to mean hash union.
my %a := { a => 1, b => 2 };
my %b := { b => 3, c => 4 };
%a | %b; # { a => 1, b => 2, c => 4 }
And what's better, adverbial modifiers will allow us to give a key union
sub:
%a | %b :union{ $^a + $^b } # { a => 1, b => 5, c => 4 }
Or you could go APL and make another meta-operator:
multi sub *infix_circumfix_meta_operator:â...â ($op) {
sub (%a, %b) {
%a | %b :union{ $op($^x, $^y) }
}
}
So you get:
%a â+â %b; # { a => 1, b => 5, c => 4 }
And then:
%a â+â= @a;
Is the operator you want. But, after all that,
++Â [EMAIL PROTECTED]
Was probably the best way to do it all along.
Perl 6 frightens me. I love it.
Luke