Reputation: 947
Is it possible to declare function's arguments as atoms?
For example,
(defn make-withdraw [(atom balance)]
(fn [amount]
(if (>= @balance amount)
(do (swap! balance #(- % amount)) @balance)
"Insufficient funds")
))
Thank you!
Upvotes: 0
Views: 495
Reputation: 20934
Clojure is a dynamically typed language so the parameter is whatever you pass to it, how you use it inside the function is what counts.
So just pass an atom to the function and you are set to go:
(make-withdraw (atom 1000))
Or create the atom inside your make-withdraw
function with a let
:
(defn make-withdraw
[balance]
(let [state (atom balance)]
(fn [amount]
(if (>= @state amount)
(do (swap! state #(- % amount)) @state)
"Insufficient funds"))))
Upvotes: 3