Browse files

Isolate side-effects in set-rooms instead of load-rooms.

Explain that using a def in a function body is not OK unless it's a
function that runs only once.
  • Loading branch information...
technomancy committed Apr 17, 2009
1 parent 43895b9 commit a8c7264adb866c60ea56133ac4f9888c16966c71
Showing with 12 additions and 4 deletions.
  1. +1 −1 src/mire.clj
  2. +11 −3 src/mire/rooms.clj
@@ -28,5 +28,5 @@
(recur (read-line))))))
-(load-rooms (str (.getParent ( *file*)) "/../data/rooms/"))
+(set-rooms "data/rooms")
(def server (create-server port mire-handle-client))
@@ -1,6 +1,6 @@
(ns mire.rooms)
-(def rooms)
+(declare rooms)
(defn load-room [rooms file]
(let [room (read-string (slurp (.getAbsolutePath file)))]
@@ -11,8 +11,16 @@
:inhabitants (ref #{})}})))
(defn load-rooms [dir]
- (def rooms (reduce load-room {}
- (.listFiles ( dir)))))
+ "Given a dir, return a map with an entry corresponding to each file
+in it. Files should be maps containing room data."
+ (reduce load-room {} (.listFiles ( dir))))
+(defn set-rooms
+ "Set mire.rooms/rooms to a map of rooms corresponding to each file
+ in dir. This function should be used only once at mire startup, so
+ having a def inside the function body should be OK."
+ [dir]
+ (def rooms (load-rooms dir)))
(def *current-room*)
(def player-name)

0 comments on commit a8c7264

Please sign in to comment.