Fork me on GitHub
#core-async
<
2015-10-15
>
artemyarulin06:10:44

Hi, does anybody know why async/to-chan doesn’t create a channel with 1 element buffer, but with min(100,collLength). Performance reasons?

(defn to-chan
  "Creates and returns a channel which contains the contents of coll,
  closing when exhausted."
  [coll]
  (let [ch (chan (bounded-count 100 coll))]
    (onto-chan ch coll)
    ch))
I’m using this function very often, but just recently found out that it breaks all the laziness of my collections like:
(defn process [n] (print n) n)
(def c (async/to-chan (map process (range 10)))) ;; Prints 0 - 9

danielcompton08:10:19

@artemyarulin: to-chan is a convenience method for onto-chan, you can supply your own buffer to onto-chan that behaves as you wish

danielcompton08:10:45

to-chan is implemented with onto-chan

artemyarulin08:10:51

Yeah, thx - I know it. I just wonder why the buffer is not 1 length by default

danielcompton09:10:38

Probably just a performance thing so that it spools ahead of lazy evaluation

artemyarulin09:10:28

Ah ok, thx. I’ll use async/onto-chan then

danielcompton09:10:23

@artemyarulin: generally putting side effects in lazy sequences is an anti pattern, but it looks like what you put up was just a demo?

artemyarulin09:10:53

Yeah, I know it. It was just for the purpose of showing that laziness got lost

artemyarulin09:10:10

thx for telling in any case simple_smile