Skip to content

Instantly share code, notes, and snippets.

@christianromney
Last active December 20, 2015 01:09
Show Gist options
  • Save christianromney/6046749 to your computer and use it in GitHub Desktop.
Save christianromney/6046749 to your computer and use it in GitHub Desktop.
Without comments to test Adrian
(ns async.core
(:require [clojure.core.async :as async :refer :all]
[clojure.data.generators :as gen])
(:gen-class))
(defrecord SyncMessage [text wake])
(defmacro forever [& body]
`(while true
~@body))
(defn boring
[talk]
(let [wake (chan)
noise #(str % ": " (gen/string "*" (inc (rand-int 15))))]
(go
(doseq [n (iterate inc 1)]
(>! talk (->SyncMessage (noise n) wake))
(<! (timeout (int (* 500 (rand)))))
(<! wake)))
talk))
(defn multiplex
[& ins]
(let [out (chan)]
(go
(forever
(let [[result _] (alts! ins)]
(>! out result))))
out))
(defn -main
[& args]
(let [src (multiplex (boring (chan))
(boring (chan)))]
(dotimes [n 5]
(let [msg1 (<!! src)
msg2 (<!! src)]
(println (:text msg1))
(println (:text msg2))
(>!! (:wake msg1) true)
(>!! (:wake msg2) true)))
(println "\nYou're both boring, I'm leaving.")))
@christianromney
Copy link
Author

Try to determine:

  • Whether or not a function has side-effects
  • What the value of each function is
  • What the output of the program is
  • Any other "interesting" events in the life of the program

@christianromney
Copy link
Author

Fixed performance bug in this version

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment