The Artima Developer Community
Sponsored Link

Ruby Buzz Forum
Unmemoize

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Premshree Pillai

Posts: 478
Nickname: premshree
Registered: Mar, 2004

Premshree Pillai is a Ruby evangelist, working with Yahoo!.
Unmemoize Posted: Aug 1, 2006 1:22 AM
Reply to this message Reply

This post originated from an RSS feed registered with Ruby Buzz by Premshree Pillai.
Original Post: Unmemoize
Feed Title: Premshree's Personal Weblog
Feed URL: http://premshree.livejournal.com/data/rss
Feed Description: Premshree's Weblog
Latest Ruby Buzz Posts
Latest Ruby Buzz Posts by Premshree Pillai
Latest Posts From Premshree's Personal Weblog

Advertisement

I tried to add an unmemoize function to the memoize module I talked about earlier:

module Memoize
   def memoize(func_name)
      cache = {}
      (class<<self; self; end).send(:define_method, "#{func_name}u", method(func_name).clone)
      (class<<self; self; end).send(:define_method, func_name) do |*args| 
         return cache[args] if cache.has_key? args
         cache[args] = super    
      end     
   end
   def unmemoize(func_name)
      (class<<self; self; end).send(:define_method, func_name) do |*args| 
         eval(func_name.to_s+"u(#{args})")
      end     
   end
end

So now you can memoize a function and then, at a later point, unmemoize:

def fib(n)
   return n if n < 2
   fib(n-1) + fib(n-2)
end

include Memoize
memoize(:fib)
...
fib(15)
...
unmemoize(:fib)
...
fib(15)
...

It works — in that the function actually unmemoizes, but the function takes noticeably longer than the original function. There’s definitely something I’m not doing right — I don’t know what though.

Anyway, it’s been a long and tiring day. Later.

Read: Unmemoize

Topic: OSCON Wrapup Previous Topic   Next Topic Topic: support for plist.rb

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use