Fork me on GitHub
#core-async
<
2022-02-14
>
ghadi14:02:38

@neil.barrett16

(def log-ch (chan 25))

(defn sink!!
  "applies f to every value taken from chan" 
  [ch f] 
  (thread
    (loop [] 
      (when-some [v (<! ch)] 
        (f v)
        (recur)))))

(sink!! log-ch prn)

ghadi14:02:34

try to avoid banging on atoms, especially with reset!

Neil Barrett17:02:27

@ghadi If I used when-let above, would putting false on the ch terminate the loop?

Neil Barrett17:02:29

Is that a standard 'poison pill' when you don't want to close the channel?

ghadi17:02:41

false is a valid value to transmit through a chan, so when-some preserves that. The only invalid value is nil, which is what the chan yields when it is closed

thanks 1
Alex Miller (Clojure team)19:02:53

and you can't put nil on a chan yourself

👍 1