Another choice is to construct shapes as closures with auto-dispatch.
So a circle could be made thus, with no data structure per se:

(defn make-circle [x y r]
  (fn [method]
    (case method
      :draw (fn [color] ...)
      :rotate (fn [degrees] ...)
      :r (fn [] r)
      :x (fn [] x)
      :y (fn [] y))))

Then the API would just delegate to each object:

(defn draw [shape color]
  ((shape :draw) color))

(defn rotare [shape degrees]
  ((shape :rotate) degrees))

And is used like this:

(def circle (make-circle 5 5 10))
(draw circle)

On Mar 15, 9:26 am, Alan <a...@malloys.org> wrote:
> On Mar 14, 7:54 pm, stu <stuart.hungerf...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > I'd like to create a simple library of drawable shapes: lines, circles
> > and rectangles.  I've placed each type of shape in its own namespace
> > with functions that operate on that shape kind:
>
> > (ns myshapes.line)
>
> > (defn line ... creates new line ...)
>
> > (defn draw ... draws a line ...)
>
> > To keep things simple a line is just a vector of two points and
> > circles and rectangles are just structs.
> > I'd also like to have a sequence of shapes called a picture:
>
> > (ns myshapes.picture)
>
> > (defn picture ... returns picture sequence from args ...)
>
> > (defn draw ... draws whole picture)
>
> > What's the idiomatic way of handling a situation like this in
> > Clojure?  Do I need to use richer data structures than vectors and
> > structs for the shapes so they carry some kind of type information?  I
> > can see how a draw multi-method would work if the individual shapes
> > could be distinguished, or am I going about this the wrong way?
>
> I think what you need is *less* rich data types, as I outline in my
> answer (sorry, didn't notice this part of the question or I'd have
> combined the two). Instead of special structs for each type, just have
> every shape carry information in similar ways; then you can write a
> draw-shape function that works with any old shape, not needing to know
> much about them. If you want to be more explicit you can define all
> shapes as a struct of {:points [p1 p2...], :whatever more-data, :and-
> also even-more}, but it will help your dispatch immensely if there's a
> single, coherent way to get a shape's points regardless of its types.
>
> Then if you ever have to put special logic that depends on something
> other than the points in the shape (eg circles have a radius), you can
> turn that draw-shape function into a multimethod and have the :default
> dispatch value do all the "boring" drawing.- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to