Jonathan Scott Duff wrote:
On Fri, Nov 08, 2002 at 05:30:00PM +0100, Paul Johnson wrote:

On Fri, Nov 08, 2002 at 03:04:16PM +0000, Nicholas Clark wrote:

On Fri, Nov 08, 2002 at 08:22:17PM +1100, Damian Conway wrote:

The name of the property is still under debate. Larry favours:

	sub square ( Num $n ) is same {...}

whereas others feel that:

	sub square ( Num $n ) is memoized {...}

is more appropriate.
We're looking for a word that tersely expresses has_no_side_effects_and_can_safely_have_its_results_cached_based_on_parameter_types_and_values_and_calling_context ?
The functional programmers will tell you that word would be "pure".

I like "memoize" only because perl has made me used to the term.
But "pure" works too.
To me, "pure" described the function, but it doesn't imply any optimization. "cached" or "memoized" implies optimization.

sub fib1(int $x) is pure {
return 1 if $x < 2;
return fib1($x-1) + fib($x-2);
}

print time { fib1(1000); };
print time { fib1(1000); };

could print the same time for both operations.

sub fib2(int $x) is memoize { fib1($x); }

print time { fib2(1000); };
print time { fib2(1000); };

should return a long time for the first call, and a very short time for the second call.

Actually, I can think of another use for 'pure', as an optimization hint.

sub foo {
for 1..1_000_000 -> {
an_impure_fibonacci($_);
}
return 10;
}

versus

sub bar {
for 1..1_000_000 -> {
a_pure_fibonacci($_);
}
return 10;
}

Both foo() and bar() return 10, and both do a similar amount of work computing 1000000 fibonacci numbers. But I can see that the optimizer, seeing that bar uses a pure version of fibonacci knows that it is side-effect free, so optimizes sub bar {...} to the much faster

sub bar {10};

(I still don't understand the rules for when and where the semi is expected).

I'm not sure that "memoize" works for that. Imagine:

sub opendb($dsn, $user, $password) is memoize { ... };

-Scott




Reply via email to