|
Posted by Daniel E. Macks on April 2, 2005, 1:15 am
Please log in for more thread options
I have an expensive function my_cmp that I'd like to Memoize. Is
there any way I can take advantage of the property that
my_cmp($a,$b) == - my_cmp($b,$a)
so if my_cmp is called with the same args as before but swapped I
don't have to redo the "actual" my_cmp function?
Conceptually I'd like to either check if the swapped-arg form has
already been Memoized:
my_cmp {
if ( Memoize::seen('my_cmp', [reverse @_]) ) {
$result = -&my_cmp(revese @_);
} else {
$result = # expensive operations
}
return $result;
}
Or force the swapped-arg form into the Memoize cache:
my_cmp {
my $result = # expensive operations
Memoize::cache('my_cmp', [reverse @_], -$result);
return $result;
}
Is there a clean way to do either of these solutions, or is there some
other existing solution I haven't found?
dan
--
Daniel Macks
dmacks@netspace.org
http://www.netspace.org/~dmacks
|