Here is something similar pulled from bagotricks "1.5.2", using Java's
linked blocking queue;
https://github.com/thebusby/bagotricks/blob/master/src/bagotricks.clj#L204-L238

It uses fold instead of reduce to run in parallel, so has a slightly
different use case than above.


On Mon, Aug 26, 2013 at 10:14 AM, Mikera <mike.r.anderson...@gmail.com>wrote:

> Nice idea Jozef!
>
> Hmmm.... this is another example of why nil-as-end-of-channel is a
> slightly problematic design decision for core.async: it makes this kind of
> code much more fiddly.
>
>
> On Monday, 26 August 2013 01:47:14 UTC+8, 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.
>

-- 
-- 
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.

Reply via email to