develooper Front page | perl.perl6.language | Postings from October 2001

Re: Are .key and .value right for Pairs?

Thread Previous | Thread Next
From:
Damian Conway
Date:
October 6, 2001 19:06
Subject:
Re: Are .key and .value right for Pairs?
Message ID:
200110070206.MAA51751@indy05.csse.monash.edu.au
Very nice. And yes, too many brackets of various kinds.
Also $this doesn't really describe what it stores.
Maybe $first would be better?

And we can also optimize the performance by making the lexicals
constant (so the optimizer can hard-wire the method names into the
closure). That gives us:

        method AUTOVIVIFY (&default, $name) {
                if ($name =~ m/^c([ad])([ad]+)r$/) {
                        my $first is const = "c$1r";
                        my $next  is const = "c$2r";
                        return sub { .$first.$next() };
                }
        }

Very nice indeed.

Of course, if we're really golfing here, we could probably push the
envelope quite a bit further, since $1, $2, etc. are *lexical* in Perl 6
and there's the scalar context specifier (C<$(...)>) that enables us to
interpolate strings directly as method refs. Oh, and those named
parameters take up *far* too many precious golf strokes. 

All of which leaves us with a one-liner:

  method AUTOVIVIFY{pop=~/^c([ad])([ad]+)r$/??sub{.$("c$1r").$("c$2r")}::undef}


Scary! ;-)

Damian

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About