Skip to content

Instantly share code, notes, and snippets.

@quux00
Created January 2, 2013 03:37
Show Gist options
  • Save quux00/4431922 to your computer and use it in GitHub Desktop.
Save quux00/4431922 to your computer and use it in GitHub Desktop.
(ns thornydev.go-lightly.boring.generator-lamina
(:require
[thornydev.go-lightly.util :refer [go& with-channel-open]]
[lamina.core :refer [channel enqueue close
closed? read-channel]]))
(defn- boring [msg]
(let [ch (channel)]
(go& (loop [i 0]
(when-not (closed? ch)
(let [status (enqueue ch (str msg " " i))]
(when-not (= :lamina/closed! status)
(Thread/sleep (rand-int 1000))
(recur (inc i)))))))
ch))
(defn single-generator []
(with-channel-open [ch (boring "boring!")]
(dotimes [_ 5] (println "You say:" @(read-channel ch))))
(println "You're boring: I'm leaving."))
(defn multiple-generators []
(with-channel-open [joe (boring "Joe")
ann (boring "Ann")]
(dotimes [_ 10]
(println @(read-channel joe))
(println @(read-channel ann))))
(println "You're boring: I'm leaving."))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment