Reputation: 14258
this is motivated by the 'art of the propagator' paper by Radul and Sussman at:
http://web.mit.edu/~axch/www/art.pdf
when they are building a compound progator
, they say:
a compound propagator is implemented with a procedure that will construct the propagator’s body on demand. We take care that it is constructed only if some neighbor actually has a value, and that it is constructed only once
the code on page 10 is:
(define (compound-propagator neighbors to-build) (let ((done? #f) (neighbors (listify neighbors))) (define (test) (if done? ’ok (if (every nothing? (map content neighbors)) ’ok (begin (set! done? #t) (to-build))))) (propagator neighbors test)))
How do we do this using clojure's persistent data structures?
a simplified version of this maybe:
(def m {:a (delayed (some-object-constructor))})
where (:a m)
constructs the object on the first call and gives
and then subsequent calls to (:a m)
will access the object.
its sort of like memoize but on values rather than functions..
Upvotes: 2
Views: 102
Reputation: 3378
As far as delayed execution, delay might be a place to start. It could be used to evaluate the constructor only on the first dereference. It might look something like this:
(defn some-object-constructor
[]
(println "Making something!")
:something)
(def m {:a (delay (some-object-constructor))})
(println "Doing some intermediate work.")
(println (deref (:a m)))
Upvotes: 1