Created
August 29, 2010 15:15
-
-
Save stoyle/556368 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
(defn transfer [from-account to-account amount] | |
(dosync | |
(if (> amount @(:balance from-account)) | |
(throw (new Exception "Not enough money!"))) | |
(alter (:balance from-account) - amount) | |
(alter (:balance to-account) + amount))) | |
(defrecord Account [account-no balance]) | |
(def account-a (Account. 1 (ref 1000))) | |
(def account-b (Account. 2 (ref 800))) | |
(transfer account-a account-b 300) | |
@(:balance account-a) | |
=> 700 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment