Ruby Idiom for dynamic programming? (request for comments)

Discussion in 'Ruby' started by Lars Christensen, Jul 2, 2008.

  1. 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
     
    Lars Christensen, Jul 2, 2008
    #1
    1. Advertising

  2. Lars Christensen

    Ron Fox Guest

    @@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
     
    Ron Fox, Jul 2, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    On Wed, Jul 2, 2008 at 7:42 AM, Ron Fox <> 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/
     
    Rick DeNatale, Jul 2, 2008
    #3
  4. 2008/7/2 Lars Christensen <>:
    > 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
     
    Robert Klemme, Jul 2, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,178
  2. Monk
    Replies:
    10
    Views:
    1,538
    Michael Wojcik
    Apr 20, 2005
  3. Daniel Lidström
    Replies:
    15
    Views:
    663
    Brendon Costa
    Oct 31, 2007
  4. Replies:
    4
    Views:
    648
    Dr John Stockton
    Jun 3, 2006
  5. ctx2002

    javascript programming idiom

    ctx2002, May 30, 2008, in forum: Javascript
    Replies:
    8
    Views:
    156
    Jorge
    Jun 1, 2008
Loading...

Share This Page