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
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to