clojure - Why do core.async go blocks return a channel? -


i understand 'go blocks' (whether go or go-loop or possibly other constructs) return channel. have never understood channel's purpose. i'd know how use it. perhaps i'm creating channels when don't need be.

i use return channel of go-block handle can pass function (not macro) wants synchronize completion of go-block. alternatively, can preform blocking reads on channel guarantee when execution of go-block has completed.

here simple example (not meant used production code compute sum) two-way parallelization:

(defn par-sum [coll]   (let [half-n (/ (count coll) 2)         [left right] (split-at half-n coll)         left-sum-chan (async/go (core/reduce + 0 left))         right-sum (core/reduce + 0 right)         left-sum (async/<!! left-sum-chan)]     (+ left-sum right-sum))) 

in example, compute left , right sums in parallel. since need left sum compute overall sum, have wait on result , retrieve result of go-block.


Comments