(defn make-account [initial-balance] (let [bal (ref initial-balance) withdraw (fn [amount] (dosync (alter bal #(- % amount)))) deposit (fn [amount] (dosync (alter bal (partial + amount)))) amount (fn [] (deref bal)) reset (fn [] (dosync (ref-set bal initial-balance)))] (fn [meth & args] (cond (= meth :withdraw) (withdraw (first args)) (= meth :deposit) (deposit (first args)) (= meth :amount) (amount) (= meth :reset) (reset)))))
Overall, it was a fun little bit of code to write! It has a constructor (make-account), a private variable with mutation (bal) and some messages it responds to (:withdraw, :deposit, :amount and :reset).
Here's an example of it being used
(def account (make-account 1000)) (println (account :amount)) ; -> 1000 (account :withdraw 75) (println (account :amount)) ; -> 1925 (account :reset) (println (account :amount)) ; -> 1000
No comments:
Post a Comment