I am working on my first (non-trivial) Clojure program I don't really feel comfortable with how I am declaring all my mutable state globally. For example:
(def next-blocks (atom []))
(def num-next-blocks 1)
(def is-game-over (atom false))
(def user-name (atom (str)))
(def hs-xml (atom nil))
Since I use C a lot at work I came up with the idea of using common C-style encapsulation techniques. It typically involves struct object that passed as a first argument to any "member functions" that operate on it. See udev for example.
Applying this to Clojure would result in functions that look like this (untested):
(defstruct gamestate)
(defn game-new []
(struct-map gamestate
:level (atom 0)
:score (atom 0)
;etc...
))
(def game-get-score [game]
@(game :score))
(defn game-set-score [game new-score]
(reset! (game :score) new-score))
(defn game-get-level [game]
@(game :level))
(defn game-inc-level [game]
(swap! (game :level) inc))
; etc...
I think it would definitely be a step forward to the global defines that I'm using currently.
So is this the recommended way to go? Or is there a more standard Clojure way?
Update
I'm currently using Clojure 1.1.0.