Last active
December 20, 2015 11:59
-
-
Save gerritjvv/6127385 to your computer and use it in GitHub Desktop.
This pattern helps to convert a connected resource (i.e. a resource accessed via some kind of connection) into a lazy sequence. The idea is to fill a in-memory buffer with data from a connection that is opened and closed. Then consume of the buffer, and when the buffer is empty fill it again with another connection.
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
(defn buffered-select [f-select init-pos] | |
"Creates a lazy sequence of messages for this datasource" | |
(letfn [ | |
(m-seq [buff pos] | |
(if-let [buff2 (if (empty? buff) (f-select pos) buff)] | |
(cons (first buff2) (lazy-seq (m-seq (rest buff2) (inc pos) ))) | |
) | |
) | |
] | |
(m-seq nil init-pos) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage:
(defn select [pos](range pos))
(def l (buffered-select select 1))
(take 20 l)