Last active
February 7, 2017 01:42
-
-
Save noisesmith/abec1d9a612c5a61bde8c7fff4ba8293 to your computer and use it in GitHub Desktop.
using core.async to combine data into chunks with a timeout
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns noisesmith.chunker | |
(:require [clojure.core.async :refer [alts! <! >! timeout go-loop]])) | |
(defn launch-chunks | |
[chunk-size time-out rq-chan result-chan] | |
(go-loop | |
[acc []] | |
(let [[res c] (alts! [(timeout time-out) | |
rq-chan]) | |
fresh (= c rq-chan) | |
timed-out (and (not fresh) (seq acc)) | |
acc (if fresh | |
(conj acc res) | |
acc)] | |
(recur | |
(if (or (> (count acc) chunk-size) | |
timed-out) | |
(do (>! result-chan acc) | |
[]) | |
acc))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
repl session: