Skip to content

Instantly share code, notes, and snippets.

@hiredman
Created February 5, 2022 05:01
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiredman/1c2e76f5544752391d78ae3df84ab993 to your computer and use it in GitHub Desktop.
Save hiredman/1c2e76f5544752391d78ae3df84ab993 to your computer and use it in GitHub Desktop.
(defn async-iteration [from-process
to-process
output-channel
& {:keys [values-selector some? init]
:or {values-selector vector
some? some?}}]
(async/go
(async/>! to-process init)
(loop [value (async/<! from-process)]
(if (some? value)
(do
(doseq [item (values-selector value)]
(async/>! output-channel item))
(async/>! value to-process))
(async/close! output-channel)))))
(defn iteration
[step!
& {:keys [vsf kf some? initk bufsize xf]
:or {vsf vector
kf identity
some? some?
initk nil
bufsize 1
xf identity}}]
(let [a (async/chan)
b (async/chan)
c (async/chan bufsize xf)]
(async-iteration a b c :vsf vsf :some? some? :initk initk)
(go
(loop []
(let [ret (<! (step! (async/<! b)))]
(when-let [k (and (some? ret)
(kf ret))]
(async/>! a k)
(recur))))
(close! ch))
c))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment