Skip to content

Instantly share code, notes, and snippets.

@fahrrad
Last active October 26, 2017 09:56
Show Gist options
  • Save fahrrad/fad22e8156becda844840fab455affb8 to your computer and use it in GitHub Desktop.
Save fahrrad/fad22e8156becda844840fab455affb8 to your computer and use it in GitHub Desktop.
(ns demo-dynamic-binding
(:require [clojure.string :as s]))
(def verbs ["fishing" "eating" "flying" "drinking"
"excessive drinking" "coma drinking"
"karaoke singing"])
(defn generate-name []
(let
[qualities ["bold" "sneaky" "red" "dark"
"fair-skinned" "well-tempered" "choleric" "bald" ""]
names ["Baldrick" "Fiona" "Cheera" "Brodkin" ]
things ["beaver" "weasel" "python" "Cheeta" "programmer"]]
(format "%s, the %s %s"
(rand-nth names)
(rand-nth qualities)
(rand-nth things))))
(def ^:dynamic *name* "Hadwin")
(defn step-in-storyline []
(str *name* " goes " (rand-nth verbs)))
(defn tell-storyline []
(dotimes [_ 10]
(println (step-in-storyline))
(Thread/sleep (rand-int 1000))))
(defn tell-story []
(dotimes [_ 5]
(.start
(Thread.
#(binding [*name* (generate-name)]
(tell-storyline))))))
(defn test-storyline []
(binding [*name* "Edward, the almost-bald programmer"]
(let [step-in-epic-journey (step-in-storyline)]
(if-not
(s/starts-with?
step-in-epic-journey
"Edward, the almost-bald programmer goes")
(throw (Exception. (str "OOOOOPS! Test failed. "
step-in-epic-journey
" does not start with Edward...")))
:success))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment