Created
September 22, 2013 07:29
-
-
Save tiensonqin/6657620 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
(defstruct item :title :current-price) | |
(defstruct bid :user :amount) | |
(def ^:dynamic history ()) | |
(def ^:dynamic droid (struct item "Droid X" 0)) | |
(defn place-offer [offer] | |
(binding [history (cons offer history) | |
droid (assoc droid :current-price (get offer :amount))] | |
(println droid history))) | |
(place-offer {:user "Anthony" :amount 10}) | |
(println droid) | |
(def droid (atom (struct item "Droid X" 0))) | |
(def history (atom ())) | |
(defn place-offer [offer] | |
(reset! droid (assoc @droid :current-price (:amount offer)))) | |
(place-offer {:user "Anthony" :amount 10}) | |
(println @droid) | |
;; transactional refs | |
(def droid (ref (struct item "Droid X" 0))) | |
(def history (ref ())) | |
(defn place-offer [offer] | |
(dosync | |
(ref-set droid (assoc @droid :current-price (get offer :amount))) | |
(ref-set history (cons offer @history)))) | |
(place-offer {:user "Tony" :amount 22}) | |
(println @droid @history) | |
;; agents | |
(def history (agent ())) | |
(def droid (agent (struct item "Droid X" 0))) | |
(defn place-offer [offer] | |
(send droid #(assoc % :current-price (get offer :amount)))) | |
(place-offer {:user "Tony" :amount 34}) | |
(await droid) | |
(println @droid) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment