Clojure.core.async - using>! and <! in function call

I would like to be able to park when calling a function from the go block. Use >!and <!not work properly.

It will be appropriate.

(go (<! (chan)))

However, if we have a function call,

(defn f [c] (<! c))
(go (f (chan)))

<!is not reset by the go block, as in a function. Are there any alternatives around this? The closest I can think of is to write a macro for f, not a function, is there an alternative function instead of <!and >!that I can use for this purpose?

+4
source share
1 answer

core.async. go s-, "" .

( , ):

(defn f [c] (go (<! c)))
(go (<! (f (chan))))

puts (<!!, >!!).

> (time (dotimes [n 100000] (<!! (go (<! (let [ch (chan)] (put! ch 1) ch))))))
"Elapsed time: 1432.751927 msecs"
nil

> (time (dotimes [n 100000] (<!! (go (<! (go (<! (let [ch (chan)] (put! ch 1) ch))))))))
"Elapsed time: 1828.132637 msecs"
nil

( core.async) 30% , .

+6

Source: https://habr.com/ru/post/1651257/


All Articles