suburbia

Premshree's (品速力) Personal Weblog

etc.

Previous Entry Add to Memories Tell a Friend Next Entry
Memoize
suburbia
[info]premshree

MJD was talking about Memoize. Here it is in Ruby:

module Memoize
  def memoize(func_name)
    cache = {}
    (class<<elf; self; end).send(:define_method, func_name) { |*args| 
      return cache[args] if cache.has_key? args
      cache[args] = super     
    }       
  end     
end

There’s a lot of code in the Perl Memoize module; maybe it does other things—I haven’t really looked. Oh, and here’s this Memoize module from RAA. I’m some months late. :-)

Here’s some pictures from Portland.


Your OSCON talk is on Wednesday, right?

In your solutions for making cross-domain requests, you can also mention Flash as an option: all requests can be routed through an invisible Flash object on the page. If you have a crossdomain.xml on the server (which most services have), you're set.

Yup, I’m talking about that too.

MJD's version demonstrates just how craptastic Perl really is compared to Ruby, with that poor OO, scope mangling, mucking about with tied hashes, and list versus scalar context. Add a couple of goofball options that I doubt anyone uses (and you don't need in Ruby) and you've got a mess.

The only feature his module has over mine is the ability to "unmemoize".

- Dan

The mucking about with tied hashes is an indication of the craptacicity of the Memoize module itself, not of Perl. It is totally unnecessary and shouldn't be in there.

In fact, a lot of the crappiness of the Memoize module is just bad design.

Some of it isn't bad design; some of it really is Perl being crappy. For example, the module needs to maintain two caches for each function, rather than one, because every Perl function can have a list context return value and a scalar context return value, which are different.

I'd say that about 2/3 of the crappiness of the module is the author's fuckups, and 1/3 is Perl's fault.


I find this useful too, for large, computed data structures:

def memoized(file)
if File.exist?(file)
return Marshal.load(IO.read(file))
else
retval = yield
File.open(file, 'w') {|f|
f.print(Marshal.dump(retval))
}
retval
end
end

Caveat of singleton method

(Anonymous)

2006-07-25 03:38 pm (UTC)

Just keep in mind that if you dup the object you will loose the memoization.

gooday - its ex wisecrack...
new journal, dont ask why.
there is a purpose though - possibly not very appealing though.

I don't know if it's technically possible to ban an IP address or not. Yet radical righties remain.