Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Ruby Idiom for dynamic programming? (request for comments)

Reply
Thread Tools

Ruby Idiom for dynamic programming? (request for comments)

 
 
Lars Christensen
Guest
Posts: n/a
 
      07-02-2008
Random idea i had...

I sometimes make custom classes/method for caching output of
functions, i.e. for dynamic programming. To generalise this, i though
it would be possible to implement it like this:

class A
def func(a, b, *c)
puts "Computing... #{[a,b,c].inspect}"
return [a,b,c].hash
end
cached :func
end

My 'cached' directive is defined as:

class Module
def cached(method, cache = (@@funccache ||= {}))
orig = "uncached_#{method}"
alias_method orig, method
define_method method do |*args|
key = [ method, args ]
@@funccache[key] ||= send(orig, *args)
end
end
end

What's the scope of @@funccache? (What 'object' holds it?). Is there a
better way to do it?

Lars
 
Reply With Quote
 
 
 
 
Ron Fox
Guest
Posts: n/a
 
      07-02-2008
@@ffunccache is a class variable, not an object variable.
It is shared amongst all instance sof the class Module.

RF

Lars Christensen wrote:
> Random idea i had...
>
> I sometimes make custom classes/method for caching output of
> functions, i.e. for dynamic programming. To generalise this, i though
> it would be possible to implement it like this:
>
> class A
> def func(a, b, *c)
> puts "Computing... #{[a,b,c].inspect}"
> return [a,b,c].hash
> end
> cached :func
> end
>
> My 'cached' directive is defined as:
>
> class Module
> def cached(method, cache = (@@funccache ||= {}))
> orig = "uncached_#{method}"
> alias_method orig, method
> define_method method do |*args|
> key = [ method, args ]
> @@funccache[key] ||= send(orig, *args)
> end
> end
> end
>
> What's the scope of @@funccache? (What 'object' holds it?). Is there a
> better way to do it?
>
> Lars



--
Ron Fox
NSCL
Michigan State University
East Lansing, MI 48824-1321
 
Reply With Quote
 
 
 
 
Rick DeNatale
Guest
Posts: n/a
 
      07-02-2008
[Note: parts of this message were removed to make it a legal post.]

On Wed, Jul 2, 2008 at 7:42 AM, Ron Fox <(E-Mail Removed)> wrote:

> @@ffunccache is a class variable, not an object variable.
> It is shared amongst all instance sof the class Module.



And, since Class is a subclass of Module, ALL classes.

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      07-02-2008
2008/7/2 Lars Christensen <(E-Mail Removed)>:
> Random idea i had...
>
> I sometimes make custom classes/method for caching output of
> functions, i.e. for dynamic programming. To generalise this, i though
> it would be possible to implement it like this:


There is memoize already...

Kind regards

robert

--
use.inject do |as, often| as.you_can - without end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pimpl idiom without dynamic memory allocation Daniel Lidström C++ 15 10-31-2007 11:51 PM
ruby idiom for attribute definition? Corey Ruby 18 11-12-2004 01:53 AM
DRY ruby idiom stevetuckner Ruby 11 09-29-2004 09:18 AM
ruby idiom for parsing function arguments? zuzu Ruby 7 07-19-2004 04:16 PM
Ruby idiom for all matches in a string David Corbin Ruby 3 10-13-2003 09:16 PM



Advertisments