Last active
December 10, 2015 11:58
-
-
Save quux00/4430997 to your computer and use it in GitHub Desktop.
Clojure implementation of boring-generators.go (https://gist.github.com/4428907)
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-tq | |
(:require [thornydev.go-lightly :refer [go go& stop]]) | |
(:import (java.util.concurrent LinkedTransferQueue))) | |
(defn- channel [] | |
(LinkedTransferQueue.)) | |
(defn- boring [msg] | |
(let [ch (channel)] | |
(go (loop [i 0] | |
(.transfer ch (str msg " " i)) | |
(Thread/sleep (rand-int 1000)) | |
(recur (inc i)))) | |
ch)) | |
(defn single-generator [] | |
(let [ch (boring "boring!")] | |
(dotimes [_ 5] (println "You say:" (.take ch)))) | |
(println "You're boring: I'm leaving.") | |
(stop)) | |
(defn multiple-generators [] | |
(let [joe (boring "Joe") | |
ann (boring "Ann")] | |
(dotimes [_ 10] | |
(println (.take joe)) | |
(println (.take ann)))) | |
(println "You're boring: I'm leaving.") | |
(stop)) | |
;; ---[ Use the fire-and-forget go& macro ]--- ;; | |
(defn- boring& [msg] | |
(let [ch (channel)] | |
(go& (loop [i 0] | |
(.transfer ch (str msg " " i)) | |
(Thread/sleep (rand-int 1000)) | |
(recur (inc i)))) | |
ch)) | |
(defn multiple-generators& [] | |
(let [joe (boring& "Joe&") | |
ann (boring& "Ann&")] | |
(dotimes [_ 10] | |
(println (.take joe)) | |
(println (.take 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