{ |one, step, back| }

The Shape Example in Clojure

Contributed by Paul Barry

Clojure is a Lisp-like functional language with a very interesting story for concurrency.

Code for Clojure

File: shapes.clj

(defn move-to [shape x y] (merge shape {:x x :y y}))
(defn r-move-to [shape x y] (move-to shape (+ (shape :x) x) (+ (shape :y) y)))
(derive ::rectangle ::shape)
(derive ::circle ::shape)
(defn rectangle [x y w h] (with-meta {:x x :y y :width w :height h} {:type ::rectangle}))
(defn circle [x y r] (with-meta {:x x :y y :radius r} {:type ::circle}))
(defmulti draw (fn [shape] ((meta shape) :type)))
(defmethod draw ::rectangle [rect]
  (println (str "Draw a Rectangle at:(" (rect :x) ", " (rect :y)
       "), width " (rect :width) ", height " (rect :height))))
(defmethod draw ::circle [circle]
  (println (str "Draw a Cicrle at:(" (circle :x) ", " (circle :y)
       "), radius " (circle :radius))))
 
(def scribble [(rectangle 10 20 5 6) (circle 15 25 8)])
 
(doseq [shape scribble]
  (draw shape)
  (let [s (r-move-to shape 100 100)]
    (draw s)))
 
(draw (assoc (rectangle 0 0 15 15) :width 30))

Output

Draw a Rectangle at:(10, 20), width 5, height 6
Draw a Rectangle at:(110, 120), width 5, height 6
Draw a Cicrle at:(15, 25), radius 8
Draw a Cicrle at:(115, 125), radius 8
Draw a Rectangle at:(0, 0), width 30, height 15