Created
November 11, 2013 09:47
-
-
Save tiensonqin/7410670 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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