Another nice abuse of core.async is for the implementation of multi-reducible collections. Here is the implementation of interleave accepting/producing reducibles. Slower than lazy seqs, but hey, you can feed it with any reducible coll. BTW anybody knows the status of multi reducibles? Are they being worked on?
(defn interleave* [& colls] (reify clojure.core.protocols.CollReduce (coll-reduce [this f1] (clojure.core.protocols/coll-reduce this f1 (f1))) (coll-reduce [_ f1 init] (let [NIL (Object.) encode-nil #(if (nil? %) NIL %) decode-nil #(if (identical? NIL %) nil %) send-fn (fn [coll] (let [c (chan)] (thread (reduce (fn [r v] (>!! c (encode-nil v))) nil coll) (close! c)) c)) chans (doall (map send-fn colls))] (loop [val init s (cycle chans)] (let [c (first s) x (<!! c)] (if-not (nil? x) (let [x (decode-nil x) ret (f1 val x)] (if (reduced? ret) @ret (recur ret (rest s)))) val))))))) (reduce + (clojure.core.reducers/take 100 (interleave* (range) (clojure.core.reducers/map inc (range))))) https://gist.github.com/wagjo/6335189 JW On Sunday, August 25, 2013 7:47:14 PM UTC+2, Jozef Wagner wrote: > > Hi, > > A distinctive feature of reducers is that "reducing is a one-shot thing". > The common understanding is that reducers are fast for cases where you want > to process whole collection at once, but for infinite and lazy seqs, you > have to use good old seqs. > > With core.async, it is now easy to create a transformation which produces > lazy seq from any reducible collection. > > (defn lazy-seq* > [reducible] > (let [c (chan) > NIL (Object.) > encode-nil #(if (nil? %) NIL %) > decode-nil #(if (identical? NIL %) nil %)] > (thread > (reduce (fn [r v] (>!! c (encode-nil v))) nil reducible) > (close! c)) > (take-while (complement nil?) (repeatedly #(decode-nil (<!! c)))))) > > (def s (lazy-seq* (clojure.core.reducers/map inc (range)))) > > (first s) > > (take 100 s) > > This approach can be also extended to produce chunked seqs and chan buffer > can also be used to further tune the performance. > > JW > > -- -- 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/groups/opt_out.