Okay... here's my first attempt at Clojure code: make "units" that
know how to draw themselves:

(defmulti poly :Unit)
(defn unit [name x y] {:Unit :Piece :name name :x x :y y})
(defmethod poly :Piece [p]
    (let [x (:x p)
          y (:y p)]
      (list x,y (+ x 200),y (+ x 200),(+ y 200) x,(+ y 200))))
(defmulti draw :Unit)
(defmethod draw :Piece [p] (apply add-poly (:name p) (poly p)))
(def u (unit "Hello!" 3100 3200))

I'm using the multi-methods to make an OO type approach.  The poly
method returns a list of coordinates for the add-poly method. The draw
method uses the name property and the poly method to draw everything.
Other types of units could be defined that have different shapes.

I like how the add-poly code works like the + function-- it just keeps
rolling through the arguments until its done. (+ 1 2 3 4).  Nifty.
Hadn't thought about drawing in that way before....

(This is just cutting and pasting code from the docs and then messing
with it until the compiler stops complaining.)

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to