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

Reply via email to