sliding-buffer

added

ns
clojure.core.async

type
function

(sliding-buffer n)

Returns a buffer of size n. When full, puts will complete, and be
buffered, but oldest elements in buffer will be dropped (not
transferred).

                (require '[clojure.core.async :refer [go-loop <! >!! sliding-buffer chan]])

;; Sliding buffers can be used to discard old values on a chan

;; Define a chan with a sliding buffer of 1 (i.e. we care mainly
;; about the latest value)
(def sliding-chan (chan (sliding-buffer 1)))

;; Print the values on the chan forever
(go-loop []
  (println "Received:" (<! sliding-chan))
  (recur))

;; Put 100 events onto the chan
(dotimes [n 100]
  (>!! sliding-chan n))

;;=> Received: 0 ;; <-- see note below
;;=> Received: 99

;; Results may vary for values from 0 to n-1
;; but you should ALWAYS see 'Received: <n-1>'