Skip to content

Instantly share code, notes, and snippets.

@juergenhoetzel
Created May 3, 2012 14:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juergenhoetzel/2586256 to your computer and use it in GitHub Desktop.
Save juergenhoetzel/2586256 to your computer and use it in GitHub Desktop.
Well-Grounded Java Developer: Account example revised
(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