Skip to content

Instantly share code, notes, and snippets.

@KingMob
Last active March 23, 2022 06:26
Show Gist options
  • Save KingMob/670b3d5354f9794b2ca5f8e655f2ae49 to your computer and use it in GitHub Desktop.
Save KingMob/670b3d5354f9794b2ca5f8e655f2ae49 to your computer and use it in GitHub Desktop.
(require '[manifold.stream :as s])
(defn coerce-seq
"if the page should happen to be an Exception, wrap
it in a vector so that it can be concatenated to the
value stream"
[v]
(if (sequential? v) v [v]))
(defn concat-seq-s
"concat a stream of seqs onto a stream of plain values"
[stream-s]
(let [out (s/stream)]
(s/connect-via
stream-s
#(s/put-all! out (coerce-seq %))
out)
out))
(def rand-v-stream (s/->source (repeatedly #(vector (rand-int 100) (rand-int 100) (rand-int 100)))))
(def oome-stream (s/transform
(mapcat coerce-seq)
rand-v-stream))
(def good-stream (concat-seq-s rand-v-stream))
@(s/reduce (fn [cnt _]
(if (> cnt 10000000)
(reduced cnt)
(inc cnt)))
0
oome-stream)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment