Click here to get back home

Accessing the Memoize cache

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Accessing the Memoize cache Daniel E. Macks 04-02-2005
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



Posted by Felix Geerinckx on April 2, 2005, 4:49 am
Please log in for more thread options


On 02/04/2005, Daniel E. Macks wrote:

> 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?

You could write

sub my_cmp {
my ($x, $y) = @_;
return $x<$y ? my_cmp2($x, $y) : -my_cmp2($y, $x);
}

and then Memoize my_cmp2.
--
felix


Similar ThreadsPosted
HTTP::Cache module September 26, 2004, 8:37 am
file upload, multipart form, and cgi::cache April 21, 2006, 7:01 am
IIS + Cache + W2003 + HTML header problem May 8, 2006, 8:24 am
problems with Memoize::Storable February 22, 2007, 4:54 pm
manually expire an element in Memoize (also on comp.lang.perl.misc) February 14, 2007, 5:52 pm
install HTML::Template - Problem reading cache file / Bad file number July 24, 2004, 7:55 pm
Accessing listbox using perl? May 16, 2007, 7:48 pm
SSH Modules: Accessing CLI box using SSH and not getting the return output?? November 12, 2006, 11:48 pm
accessing a hash made by iCal::Parser February 6, 2006, 11:16 am
Problem in accessing DLL function from perl module. July 31, 2007, 4:52 am

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap