Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

semi-OT: Lisp code

by hding (Chaplain)
on Apr 01, 2003 at 18:06 UTC ( #247295=note: print w/ replies, xml ) Need Help??


in reply to OO, Class::MethodMaker, and get_set

I'd do it something like this in Lisp (not very tested):

(defclass sum () ((a :accessor a :initarg :a :initform 0) (b :accessor b :initarg :b :initform 0) (c :reader c))) (defmethod compute ((self sum)) (setf (slot-value self 'c) (+ (a self) (b self)))) (defmethod initialize-instance :after ((self sum) &key &allow-other-ke +ys) (compute self)) (defmethod (setf a) :after (val (self sum)) (declare (ignore val)) (compute self)) (defmethod (setf b) :after (val (self sum)) (declare (ignore val)) (compute self))

Content of marginal relevance to Perl: Maybe there's something out there that implements something akin to Lisp's :after methods in Perl that would do what you want. I don't know, though.


Comment on semi-OT: Lisp code
Download Code
Re: semi-OT: Lisp code
by runrig (Abbot) on Apr 01, 2003 at 18:31 UTC
    Maybe there's something out there that implements something akin to Lisp's :after methods in Perl that would do what you want.
    Well, there is Hook::LexWrap. But if what I read elsewhere in this thread is true (that we assume the computation of 'C' is expensive and we'd like to cache it), then you'd just want to clear the 'C' cache when you change 'A' or 'B', and when you request 'C', just return the cached value if it exists, else compute, cache, and return it.

      This is true. I was just trying to implement something as close as possible to what the original poster asked for with respect to his toy problem, which was recomputation of c whenever a or b was set. Of course it's be easy to modify things to use a cache; e.g. set c to nil (or some other convenient marker) whenever a or b is set and put a :before method on the method c that computes and sets the slot c it if it is nil.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://247295]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-10-21 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (94 votes), past polls