On Fri, Jan 22, 2010 at 11:41 AM, Laurent PETIT <laurent.pe...@gmail.com>wrote:
> David, > > maybe I don't understand your example well, but anyway, here are my > remarks / concerns: > > * You're example is not at all exempt from multithreading: you use > futures, and in the real scenario, the calls to deliver would have to > occur from a thread able to pull from or be notified by the progress > of the tests > Sure deliver would happen on the main thread. Basically the idea is that the main thread is never blocked. Blocking happens on other threads. > * If one wants all the graphical values to be update "on the fly", > then one will have to place the calls to deref in independent threads, > wouldn't it be too heavy to have so many threads (perhaps 100, 10000 > ?) just for updating a single node of a tree in the overall > application ? > My technique certainly isn't efficient but it does let you engage in a certain style of programming that I think can sometimes be useful. My example is actually taken from a real use case I wrote in JavaScript after understanding how promises worked in Clojure. ;; 3 ms (time (reduce + (range 1000))) ;; 53ms (time (let [r (take 1000 (repeatedly promise))] (future-out (println "value:" (reduce + (map deref r)))) (dotimes [x 1000] (deliver (nth r x) x)))) If we're just pushing stuff out to UI, 53ms is certainly acceptable no? > > > > 2010/1/22 David Nolen <dnolen.li...@gmail.com>: > > They are actually useful even outside the context of concurrency. For > > example perhaps you have a recursive data structure that represents a > series > > of unit tests and unit test suites. You would like to do two things: > > 1. Render out the nested structure of the tests that will be executed for > > the user > > 2. Render out the test results as they complete > > With out promises you would have to traverse this data structure twice. > Once > > to render it for the user and a second time to show the results. With > > promises you only need to traverse the data structure one time. In the > > traversal you create promises for the test results as well as created a > > flattened list of the tests to run. > > In the following imagine that each test returns 0 or 1 depending on > > pass/fail (I haven't bothered to make a recursive data structure here, > but > > the general point should be clear I hope). > > (def x (promise)) > > (def y (promise)) > > (def z (promise)) > > (def passed (promise)) > > (defmacro future-out [& body] > > `(let [out# *out*] > > (future > > (binding [*out* out#] > > ~...@body)))) > > (future-out > > (deliver passed (reduce + (map deref [x y z]))) > > (println "passed:" passed)) > > (future-out (println "x:" @x)) > > (future-out (println "y:" @y)) > > (future-out (println "z:" @z)) > > (deliver x 1) > > (deliver y 0) > > (deliver z 1) > > Pretty useful I say :) > > On Thu, Jan 21, 2010 at 6:19 AM, Baishampayan Ghose < > b.gh...@ocricket.com> > > wrote: > >> > >> Hello, > >> > >> I am trying to understand the use-cases of the new promise/deliver > >> feature in Clojure. I have tried using them, and they seem to be > >> pretty straight-forward to use, but unfortunately I haven't been able > >> to understand its use-cases. > >> > >> It would be great if someone pointed out some example usage of > >> promise/deliver. > >> > >> Regards, > >> BG > >> > >> -- > >> Baishampayan Ghose > >> > >> -- > >> 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<clojure%2bunsubscr...@googlegroups.com> > >> For more options, visit this group at > >> http://groups.google.com/group/clojure?hl=en > > > > -- > > 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<clojure%2bunsubscr...@googlegroups.com> > > For more options, visit this group at > > http://groups.google.com/group/clojure?hl=en > > -- > 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<clojure%2bunsubscr...@googlegroups.com> > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > -- 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