Luke Palmer wrote:
Mailing-List: contact [EMAIL PROTECTED]; run by ezmlm
From: Luke Palmer <[EMAIL PROTECTED]>
Cc: [EMAIL PROTECTED]
Date: Thu,  7 Nov 2002 13:49:14 -0700 (MST)
X-SMTPD: qpsmtpd/0.12, http://develooper.com/code/qpsmtpd/


Date: Thu, 07 Nov 2002 20:48:50 +1100
From: Damian Conway <[EMAIL PROTECTED]>

we could make it lazy thus:

	sub a_pure_func(Num $n) is lazy returns Num {
	    return $n ** $n
	}

which would cause any invocation of C<a_pure_func> to cache
its arguments (probably in a closure) and return a "proxy"
Num that carries out the computation only when the proxy is
evaluated.
sub a_pure_func(Num $n) returns Num {
class is Num {
method FETCH { $n * $n } }.new }

Whoops, I mean...

    sub a_pure_func(Num $n) returns Num {
        class is Num {
            has Num $cache;
            method FETCH { $cache //= $n * $n } } }

Wow, I've never seen such a compact implementation of such a thing.  I
love you, Perl 6  <3]
This is better, but it doesn't deal with STORE.... or does it? Nope, it doesn't. You need to delegate to $cache.

Hmmm...

sub a_pure_func(Num a) returns Num {
class is Num {
has Num $cache is delegate;
method FETCH {$cache //= $n * $n } }.new }

Or...

sub a_pure_func(Num $a) returns Num {
class is Num (
has Num $cache;
method FETCH { .cache //= $n * $n }
method STORE(Num $x) { .cache = $x } }.new }

I think....


Luke





Reply via email to