Last active
December 10, 2015 12:08
-
-
Save quux00/4431580 to your computer and use it in GitHub Desktop.
Alternative to https://gist.github.com/4430997 using the Clojure lamina library.
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&]] | |
[lamina.core :refer [channel enqueue close | |
closed? read-channel]])) | |
;; this version is REPL-friendly since the deamon go routine | |
;; will exit when the channel is closed | |
(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 [] | |
(let [ch (boring "boring!")] | |
(dotimes [_ 5] (println "You say:" @(read-channel ch))) | |
(close ch)) | |
(println "You're boring: I'm leaving.")) | |
(defn multiple-generators [] | |
(let [joe (boring "Joe") | |
ann (boring "Ann")] | |
(dotimes [_ 10] | |
(println @(read-channel joe)) | |
(println @(read-channel ann))) | |
(close joe) | |
(close 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