Skip to content

Instantly share code, notes, and snippets.

@aleksandersumowski
Created November 5, 2019 15:16
Show Gist options
  • Save aleksandersumowski/314c298aa16583db331a425e339774ff to your computer and use it in GitHub Desktop.
Save aleksandersumowski/314c298aa16583db331a425e339774ff to your computer and use it in GitHub Desktop.
a-d-d with simple reduced checks
(defn a-d-d [delay-size]
(fn [rf]
(let [state (volatile! [])]
(fn
([] (rf))
([result]
(let [buffer @state]
(-> (if (seq buffer)
(rf result buffer)
result)
(rf))))
([result input]
(if (reduced? input)
input
(let [buffer (conj @state input)]
(if (< (count buffer) delay-size)
(do (vreset! state buffer)
result)
(do (vreset! state [])
(rf result buffer))))))))))
;; under what scenarios ^^ fails?
(slow-into [] (comp (take 7)
(a-d-d-2 3)
(take 3))
(range 10))
;; this returns #object [clojure.lang.Reduced 1389120027 {:status :ready, :val #object [clojure.lang.Reduced 536311285 {:status :ready, :val [[0 1 2] [3 4 5] [6]]}]}] for both versions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment