Skip to content

Instantly share code, notes, and snippets.

@frankiesardo
Created February 13, 2018 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankiesardo/feff98075dc3795bba07af3cbb528391 to your computer and use it in GitHub Desktop.
Save frankiesardo/feff98075dc3795bba07af3cbb528391 to your computer and use it in GitHub Desktop.
(defn batch [in out max-time max-count]
(let [lim-1 (dec max-count)]
(async/go-loop [buf [] t (async/timeout max-time)]
(let [[v p] (async/alts! [in t])]
(cond
(= p t)
(do
(async/>! out buf)
(recur [] (async/timeout max-time)))
(nil? v)
(if (seq buf)
(async/>! out buf))
(== (count buf) lim-1)
(do
(async/>! out (conj buf v))
(recur [] (async/timeout max-time)))
:else
(recur (conj buf v) t))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment