Skip to content

Instantly share code, notes, and snippets.

@shlomiv
Created August 21, 2013 15:26
Show Gist options
  • Save shlomiv/6295921 to your computer and use it in GitHub Desktop.
Save shlomiv/6295921 to your computer and use it in GitHub Desktop.
(defn first-out [& fns]
(let [fs (atom [])
ret (promise)
terminate (fn [x] (println "cancling.." ) (doall (map future-cancel @fs)) (deliver ret x))]
(reset! fs (doall
(map (fn [x] (future-call #(terminate (x)))) fns )))
@ret
))
(defn wait-for [n s]
"this time, return the value"
(fn [] (print "start...") (flush) (Thread/sleep n) (print s) (flush) s))
(first-out (wait-for 1000 "long") (wait-for 500 "short"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment