Skip to content

Instantly share code, notes, and snippets.

@razum2um
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razum2um/d3255595a481ceae5745 to your computer and use it in GitHub Desktop.
Save razum2um/d3255595a481ceae5745 to your computer and use it in GitHub Desktop.
;; converter below creates a chan and returns it
;; but go-loop itself creates a chan
;;
;; 1) is it memory safe?
;; 2) do I need to call `close!` on go-loop created channel?
user=> (def in (chan))
#'user/in
user=> (defn converter [ch-in] (let [ch-out (chan)] (go-loop [m (<! ch-in)] (if m (>! ch-out (inc m)) (close! ch-out))) ch-out))
#'user/converter
user=> (def out (converter in))
#'user/out
user=> (async/thread (prn "out: " (<!! out)))
#<ManyToManyChannel clojure.core.async.impl.channels.ManyToManyChannel@3dca9ec3>
user=> (>!! in 5)
true
"out: " 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment