Skip to content

Instantly share code, notes, and snippets.

Last active Aug 29, 2015
What would you like to do?
(defn paged-http-get
(paged-http-get url 1))
([url page]
(let [sink (s/stream)]
(d/loop [page page]
(d/chain (http-get url {:query-params {"page" page}})
(fn [response]
(let [body (:body response)]
(if (seq body)
(s/put-all! sink body)
(s/close! sink))))
(fn [puts]
(when puts
(d/recur (inc page))))))
(defn stream-take-count [limit s]
(let [s' (stream/stream)
count (atom 0)]
(stream/connect-via s
(fn [msg]
(if (>= @count limit)
(stream/close! s')
(d/success-deferred false))
(swap! count inc)
(stream/put! s' msg))))
(stream/source-only s')))
(let [s (paged-http-get "")]
(->> s
(stream/map :name)
(stream-take-count 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment