Created
May 3, 2012 14:55
-
-
Save juergenhoetzel/2586256 to your computer and use it in GitHub Desktop.
Well-Grounded Java Developer: Account example revised
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
(defn make-account [name balance] | |
{:name name :balance balance}) | |
(defn withdraw [account n] | |
(update-in account [:balance] - n)) | |
; Single-Treaded withdraw/loop | |
(loop [a (make-account "Ben" 5000)] | |
(Thread/sleep 1) | |
(if (pos? (:balance a)) | |
(recur (withdraw a 1)) | |
a)) | |
(defn account-depositor [account-ref] | |
(Thread/sleep 1) | |
(if (dosync | |
(if (pos? (:balance @account-ref)) | |
(pos? (:balance (alter account-ref withdraw 1))))) | |
(recur account-ref))) | |
(def my-acc (ref (make-account "Ben" 5000))) | |
(apply pcalls (repeat 5 (partial account-depositor my-acc))) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment