Dear Timo,

I'm almost done with categorize. I stole similar example from rakudo
tests but I can't get rid off array  on end. If I remove from map []
it stops working.

PS. I barely understand code I wrote anyway :)


$ perl6 ~/xxxxxx.pl
[A.new(a => "a", b => "22")]
Reference
A.new(a => "a", b => "22")



use v6;

class A { has $.a; has $.b };
my @array = A.new(a=>'a', b=>'11'),
    A.new(a=>'a', b=>'22'),
    A.new(a=>'v', b=>'33'),
    A.new(a=>'w', b=>'44'),
    A.new(a=>'v', b=>'55');


my %h = categorize({map {[.a , .b]}, $_},@array);

say %h<a><22>.perl;

my %hash;
for @array -> $elem {
    %hash{$elem.a}{$elem.b} =$elem;
}


say "Reference\n" ~ %hash<a><22>.perl;

On Fri, Jun 13, 2014 at 9:08 PM,  <t...@wakelift.de> wrote:
> On 06/13/2014 08:15 PM, Kamil Kułaga wrote:
>> Ok got it. But solution is neither more readable nor faster (IMHO only
>> -> I didn't benchmark it)
>>
>>
>> class A { has $.a; has $.b };
>> my @array = A.new(a=>'a', b=>'11'),
>>     A.new(a=>'a', b=>'22'),
>>     A.new(a=>'v', b=>'33'),
>>     A.new(a=>'w', b=>'44'),
>>     A.new(a=>'v', b=>'55');
>>
>> my %h = @array.map({
>>     my $var = .a;
>>     $var => %(@array.grep({.a eq $var}).map({.b => $_}))
>>            });
>>
>> say %h<a>.perl;
>>
>> my %hash;
>> for @array -> $elem {
>>     %hash{$elem.a}{$elem.b} =$elem;
>> }
>>
>>
>> say "Reference\n" ~ %hash<a>.perl;
>
> Dear Kamil,
>
> the method "categorize", as specced in
> http://perlcabal.org/syn/S32/Containers.html#categorize will allow you
> to return a parcel from the "categorization" you provide (such as -> $_
> { (.a, .b) }) and build a multi-leveled hash.
>
> Sadly, I wasn't able to make it work right away.
>
> Cheers,
>   - Timo
>



-- 
Pozdrawiam

Kamil Kułaga

Reply via email to