I think you're really talking about reductions, not reduce. In sequence 
form, like:

user=> (reductions + [0 1 2 3 4 5])
(0 1 3 6 10 15)

There is not currently a reductions transducer although there is a ticket 
proposing to add one: http://dev.clojure.org/jira/browse/CLJ-1903

You don't have to wait for that of course, you could just use the 
reductions transducer defined in that ticket.

(require '[clojure.core.async :as a :refer [<!!]])

(defn reductions-with
  ([f init]
   (fn [rf]
     (let [state (volatile! init)]
       (fn
         ([] (rf))
         ([result] (rf result))
         ([result input]
          (if (reduced? @state)
            @state
            (rf result (unreduced (vswap! state f input))))))))))

;; create the summarizing chan 
(def ch (a/chan 1 (reductions-with + 0)))

;; in background thread pump numbers in
(future (a/onto-chan ch (range 6)))

;; drain results
(<!! (a/reduce conj [] ch))
;;=> [0 1 3 6 10 15]



On Wednesday, August 10, 2016 at 8:26:39 AM UTC-5, JokkeB wrote:
>
> Hi,
>
> I'm wondering why reduce doesn't return a transducer like map does. For 
> example I'd like to this:
>
> (async/chan 1 (reduce +))
>
> I could use async/reduce but it doesn't provide a result before the source 
> channel closes, I want to have a streaming result.
>
> What is the philosophy behind not having reduce return a transducer when 
> no collection is passed to it? How should I go about doing a streaming 
> stateful reducer, like in my example?
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to