Skip to content

Instantly share code, notes, and snippets.

@gerritjvv
Last active December 20, 2015 11:59
Show Gist options
  • Save gerritjvv/6127385 to your computer and use it in GitHub Desktop.
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.
(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)
)
)
@gerritjvv
Copy link
Author

Usage:

(defn select [pos](range pos))

(def l (buffered-select select 1))

(take 20 l)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment