Today I wrote some perl 5 code that looked like this:

        my %index_by_x;
        my %index_by_y;
        my %index_by_z;

        foreach my $thing (@things){
                ( $index_by_x{$thing->x_value} ||= 
Set::Object->new)->insert($thing);
                ( $index_by_y{$thing->y_value} ||= 
Set::Object->new)->insert($thing);
                ( $index_by_z{$thing->z_value} ||= 
Set::Object->new)->insert($thing);
        }

I feel this code is pretty unreadable. Luke and I tossed some ideas
for how this should look in perl 6:


from #perl6:

        class Foo will autovivify { Foo.new } {
                ...
        }

        my Foo %hash;

OR

        my %hash will autovivify { Foo.new };

and then:

        %hash<key_that_doesn't_exist>.moose; # works

or with my code:

        my &mk_set_object = { Set::Object.new }

        my %index_x will autovivify(&mk_set_object);
        my %index_y will autovivify(&mk_set_object);
        my %index_z will autovivify(&mk_set_object);

or

        my class ViviSet will autovivify { $?CLASS.new }

        my ViviSet %index_x;
        my ViviSet %index_y;
        my ViviSet %index_z;

and then

        for @things -> $thing {
                $index_x{ $thing.x_value }.insert($thing);      
                $index_y{ $thing.y_value }.insert($thing);      
                $index_z{ $thing.z_value }.insert($thing);      
        }

which makes the 'x_value' etc calls much more obvious, and thus good
for readability.

-- 
 ()  Yuval Kogman <[EMAIL PROTECTED]> 0xEBD27418  perl hacker &
 /\  kung foo master: /methinks long and hard, and runs away: neeyah!!!

Attachment: pgpT6fmyMD2Sl.pgp
Description: PGP signature

Reply via email to