On Wed, Mar 28, 2001 at 11:59:19AM -0500, John Porter wrote:
> I mean, in general, it would be nice if there were a way to have
> perl memoize for us, rather than have to arrange it ourself.

Again with the over-specific solutions! What you seem to want is for 
(for instance)

    sub foo :memoize  # Colon Rule conformance
    {
        # THING
    }

to automagically memoize the subroutine. That's the specific solution.

The general solution is "allow people to register user-defined behaviour
triggered by certain attributes", hence:

    use attribute::sub memoize => sub {
        my ($coderef, $argument) = @_;
        exists $cache{$coderef}{$argument} ? 
               $cache{$coderef}{$argument} :
               $cache{$coderef}{$argument} = $coderef->($argument);
    }

-- 
An ASCII character walks into a bar and orders a double.  "Having a bad
day?" asks the barman.  "Yeah, I have a parity error," replies the ASCII
character.  The barman says, "Yeah, I thought you looked a bit off." 
    -- from Skud

Reply via email to