philipbohun.com

Glojure by Example: Walking Channels

There is no range over a channel. Receive in a loop and stop when the value is nil. That works only if real messages are never nil, which the channel operations already require.

(require '[clojure.core.async :as async])

(def jobs (async/chan 3))
(doseq [job ["a" "b" "c"]]
  (async/>! jobs job))
(async/close! jobs)

(loop []
  (when-let [job (async/<! jobs)]
    (println "job" job)
    (recur)))
(println "closed")

when-let treats the nil from a closed channel as the end.

$ glj walking-channels.glj
job a
job b
job c
closed

Next example: Timers.