I'd like to perform a number (MAX_OPERATIONS
) of money transfers from one account to another. The accounts are stored as refs in a hash-map caller my-map
(int account-id, double balance).
The money transfer takes a "random index" from the hash map and passes it as account-from
to transfer
. account-destination
and amount
should both be fixed.
Unfortunately I can't make it work.
(defn transfer [from-account to-account amount]
(dosync
(if (> amount @from-account)
(throw (Exception. "Not enough money")))
(alter from-account - amount)
(alter to-account + amount)))
(defn transfer-all []
(dotimes [MAX_OPERATIONS]
(transfer (get mymap (rand-int[MAX_ACCOUNT]) :account-id) account-destination amount)))
((rand-nth (keys mymap)) mymap )
to do it without get. – Orpah