Skip to content

Instantly share code, notes, and snippets.

@naiquevin
Created January 8, 2015 18:10

Revisions

  1. naiquevin created this gist Jan 8, 2015.
    25 changes: 25 additions & 0 deletions async.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    (defn consume-until-timeout
    [ms ch f init & args]
    (let [tmt (timeout ms)]
    (go (loop [ret init]
    (if-let [v (first (alts! [ch tmt]))]
    (recur (apply f ret [v]))
    ret)))))


    (comment
    (let [c (chan)]
    (doseq [i (range 1 7)]
    (go (>! c (do
    (Thread/sleep (* i 1000))
    i))))
    (<!! (consume-until-timeout 3000 c conj []))))


    (comment
    (let [c (chan)]
    (doseq [i (range 1 7)]
    (go (>! c (do
    (Thread/sleep (* i 1000))
    i))))
    (consume-until-timeout 6100 c prn [])))