Skip to content

Instantly share code, notes, and snippets.

View MichaelDrogalis's full-sized avatar

Michael Drogalis MichaelDrogalis

  • Confluent
  • Seattle, WA
View GitHub Profile
(is (= (+ 40 2) 42))
A: 0
A: 1
B: 0
A: 2
A: 3
B: 1
C: 0
A: 4
B: 2
A: 5
(defn pipeline []
(let [bound 10000
m-ch (chan bound)
n-ch (chan bound)
o-ch (chan bound)]
(go (while true (put! n-ch (m (<! m-ch)))))
(go (while true (put! o-ch (n (<! n-ch)))))
(go (while true (o (<! o-ch))))
m-ch))
(defn m [x]
(send printer (fn [_] (println "A:" x)))
(Thread/sleep 500)
x)
(defn n [x]
(send printer (fn [_] (println "B:" x)))
(Thread/sleep 1000)
x)
(defn a [x]
(println "A:" x)
x)
(defn b [x]
(println "B:" x)
x)
(defn c [x]
(println "C:" x)
(defn g [x]
(throw {:a 1}))
(defn f [x]
(g x))
(with-pre-hook! #'f
(λ [y]
(prn "Got " y)))
(defn drive-forward [car speed]
(let [new-front (- (:front car) speed)]
(assoc car :front (max new-front 0))))
(defn put-into-ch [channel car]
(.put channel car))
(defn take-from-channel [channel]
(.take channel))
(defn ch->lane [{:keys [channel state] :as entity} d-fn]
(if-not (zero? (.size channel))
(add-to-lane entity (take-from-channel channel) d-fn)
entity))
(defn sim-loop! [snapshot t-fn queue speed]
(let [successor (t-fn snapshot)]
(send-off queue (constantly successor))
(Thread/sleep speed)
(recur successor t-fn queue)))
(defn advance-cars-in-lane [{:keys [state lane] :as entity}]
(assoc entity :state
(clojure.core.reducers/reduce
(partial advance (:speed lane) state) [] state)))