Created
January 8, 2016 23:52
-
-
Save rmuslimov/4bc486b864ea67e66694 to your computer and use it in GitHub Desktop.
Try to implement chunks with core.async
This file contains 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 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