lps540 <[email protected]> writes:
> I've written a small example of Conway's Game of Life using Clojure
> and Swing. Comments/critiques are welcome.
Interesting program!
(for [x (range 32) y (range 48)]
(ref-set cells
(assoc (deref cells) [x y] (= 0 (rand-int 5)))))
While you can use ref-set this way, it's much more concise to use alter:
(for [x (range 32) y (range 48)]
(alter cells assoc [x y] (= 0 (rand-int 5))))
You should use this in calc-state as well.
Also, the paint-cells function is called only for side-effects. Instead
of using dorun+map, you should use doseq:
(defn paint-cells [graphics]
(doseq [cell @cells]
(let [x (first (first cell))
y (second (first cell))
state (second cell)]
(doto graphics
(. setColor (if state Color/RED Color/WHITE))
(. fillRect (* 10 x) (* 10 y) 10 10)))))
I'm not sure if making "running" a ref makes sense here. Do you need to
coordinate change in it across threads, or will the change only happen
in one thread? If it's the former, using a thread-local var with
"binding" is probably a better choice.
Also, instead of calling (deref cells), you can use @cells for short.
-Phil
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---