On 2010-04-04, at 2:47 PM, Mark Engelberg wrote:

> I don't understand why you need to "get rid of the delay" once it has been
> updated.  Delays are cheap; why not just be consistent about having your
> data be a ref of a delay?  It will keep your code simpler, and that's well
> worth it.

That's one of the possibilities I considered. But after the first change the 
delay would always have to be forced (it cannot be left as a delay, see the 
example I've posted below). I also didn't want to expose the delay to the 
users/programmers trying to use the stuff (too ugly). The approach of defining 
a type similar to lazy-ref that can initially have a delay but later have just 
a ref is very attractive since there's a ref-like interface for the user and it 
avoids the problem shown below.

Here's a bit of code using Per's example code:

(defn bad-thing [n]
 (let [not-lazy (ref 0)
       lazy     (lazy-ref 0)
       counter  (ref 0)]

   (dotimes [i n]
     (dosync
       (ref-set counter i)
       (lazy-alter lazy (fn [x] (+ x @counter)))
       (alter not-lazy (fn [x] (+ x @counter)))))

   (println "counter: " @counter "lazy:" @lazy "not-lazy:" @not-lazy)
   (println "(* n @counter)      :" (* n @counter))
   (println "(/ (* n (- n 1)) 2) :" (/ (* n (- n 1)) 2))))

With output:

lazy-ref=> (bad-thing 200)
counter:  199 lazy: 39800 not-lazy: 19900
(* n @counter)      : 39800
(/ (* n (- n 1)) 2) : 19900
nil

You can see the (very) bad situation of the lazy-ref that depends on other refs 
whose values change. This is not just a problem with the lazy-ref, it would be 
easy to have the same effect by setting up delays that don't force previous 
values.


> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> 
> To unsubscribe, reply using "remove me" as the subject.

----
Bob Hutchison
Recursive Design Inc.
http://www.recursive.ca/
weblog: http://xampl.com/so




-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to