Created
January 2, 2013 03:37
-
-
Save quux00/4431922 to your computer and use it in GitHub Desktop.
Version 2 of https://gist.github.com/4431580
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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