Skip to content

Instantly share code, notes, and snippets.

@rmuslimov
Created January 8, 2016 23:52
Show Gist options
  • Save rmuslimov/4bc486b864ea67e66694 to your computer and use it in GitHub Desktop.
Save rmuslimov/4bc486b864ea67e66694 to your computer and use it in GitHub Desktop.
Try to implement chunks with core.async
(ns cltrace.example
(:require [clojure.core.async :refer [<! >! >!! chan go-loop]]))
(def queue (atom []))
(defn process-chan [in]
(let [out (chan)]
(go-loop [item (<! in)]
(swap! queue conj item)
(when (> (count @queue) 3)
(let [records @queue
[head rest] (split-at 3 records)]
(if (compare-and-set! queue records (apply vector rest))
(>! out head))))
(recur (<! in)))
out))
(defn bind-dummy-printer [output]
"Dummy printer for a/chan."
(go-loop [record (<! output)]
(println "Chunk were received: " record)
(recur (<! output))))
(defn example-main []
"Setup example."
(def input (chan))
(let [output (process-chan input)]
(bind-dummy-printer output)
(map #(>!! input (str %)) (range 10))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment