i.e. what do I not understand about map and/or promises.

(import '(java.util.concurrent Executors))
(def pool (Executors/newFixedThreadPool 10))

(defn sleep-for [t] (. Thread (sleep t)) t)

(defn fastest-first-map [f coll]
  (let [ps (map (fn [_] (promise)) (range (count coll)))
        a (agent ps)
        delivery-fn (fn [result]
                      (send a (fn [[p & ps] result*]
                                (deliver p result*)
                                ps)
                            result))]
    (dorun (map (fn [v]
                  (.submit pool #(delivery-fn (f v))))
                  coll))
    ps))


(defn do-it [] (fastest-first-map (fn [f] (f))
                 (map partial (repeat sleep-for) 
                              (shuffle (range 100 1000 100)))))

;; This works as expected
(time @(first (do-it)))
;;"Elapsed time: 100.986 msecs"
;; => 100

;; This doesn't return until the entire result is dereferenced 
(time (first (map deref (do-it))))
;;"Elapsed time: 900.525 msecs"
;; => 100

(chunked-seq? (do-it))
;; => false

I'm at a loss on this one.

-- 
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