Skip to content

Instantly share code, notes, and snippets.

@tiensonqin
Created November 11, 2013 09:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tiensonqin/7410670 to your computer and use it in GitHub Desktop.
Save tiensonqin/7410670 to your computer and use it in GitHub Desktop.
(defmacro futures
[n & exprs]
(vec (for [_ (range n)
expr exprs]
`(future ~expr))))
(defmacro wait-futures
[& args]
`(doseq [f# (futures ~@args)]
@f#))
(defn character
[name & {:as opts}]
(ref (merge {:name name :items #{} :health 500}
opts)))
(def smaug (character "Smaug" :health 500 :strength 400 :items (set (range 50))))
(def bilbo (character "Bilbo" :health 100 :strength 100))
(def gandalf (character "Gandalf" :health 75 :mana 750))
(defn loot
[from to]
(dosync
(when-let [item (first (:items @from))]
(alter to update-in [:items] conj item)
(alter from update-in [:items] disj item))))
(wait-futures 1
(while (loot smaug bilbo))
(while (loot smaug gandalf)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment