Skip to content

Instantly share code, notes, and snippets.

@quux00
Last active December 10, 2015 11:58
Show Gist options
  • Save quux00/4430997 to your computer and use it in GitHub Desktop.
Save quux00/4430997 to your computer and use it in GitHub Desktop.
Clojure implementation of boring-generators.go (https://gist.github.com/4428907)
(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