Advertisement
Guest User

Untitled

a guest
Sep 1st, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. (defn buffered-reduce
  2. "Return a transducer wrapping `reduction-fn` which holds on to an internal
  3. buffer volatile (initialized with nil) which will be flushed (via `reduce`)
  4. when the transducer is closed.
  5.  
  6. The `reduction-fn` should accept a combining function, an accumulating value,
  7. a current value, and a buffer volatile. This function should return a new
  8. accumulating value via 0, 1 or many calls to `(xf r new-value)`. It may also
  9. manipulate the contents of the volatile for its own use. If anything is found
  10. in the buffer when the reduction is complete, its items will be added to the
  11. reduction before closing."
  12. [reduction-fn]
  13. (fn [xf]
  14. (let [buffer (volatile! nil)]
  15. (fn
  16. ([] (xf))
  17. ([r] (let [remaining @buffer]
  18. (vreset! buffer nil)
  19. (xf (reduce xf r remaining))))
  20. ([r v]
  21. (reduction-fn xf r v buffer))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement