Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-17 Thread stu
Hi, I wanted to take the opportunity to thank the people who responded to my question on thinking beyond O-O. The replies form a very useful slice through Clojure design strategies and idiomatic use of the language. Thanks! Stu -- You received this message because you are subscribed to the Go

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Ken Wesson
On Tue, Mar 15, 2011 at 12:58 PM, Armando Blancas wrote: > 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] ...) >      :ro

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Ken Wesson
On Mon, Mar 14, 2011 at 11:15 PM, Daniel Solano Gomez wrote: > This method works fairly well, and you can even use it to define > protocols for types you don't control, such as classes from a Java API. > > If you need some more complicated form of dispatch for polymorphism, > there is the multimet

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Alan
On Mar 15, 10:50 am, Mark Engelberg wrote: > On Tue, Mar 15, 2011 at 10:40 AM, Alan wrote: > > Seconded. I often know ahead of time I'll want to extend to > > multimethods eventually, but sometimes I don't. Because multimethods, > > protocols, and plain-old-functions look the same to the caller,

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Mark Engelberg
On Tue, Mar 15, 2011 at 10:40 AM, Alan wrote: > Seconded. I often know ahead of time I'll want to extend to > multimethods eventually, but sometimes I don't. Because multimethods, > protocols, and plain-old-functions look the same to the caller, you > can write your code whatever way is most stra

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Alan
On Mar 15, 10:30 am, Daniel Solano Gomez wrote: > In the end, the it all depends on what fits best with what you are > doing.  One approach is to simply avoid using multimethods or > polymorphism as you start developing.  Instead, you can use a custom > dispatch using if or cond in your polymorphi

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Luiz Augusto Moreira Costa
Hi stu, take a look in clojure protocols. I think that this can help you. http://clojure.org/protocols Luiz Costa On Mon, Mar 14, 2011 at 11:54 PM, stu wrote: > Hi, > > I'd like to create a simple library of drawable shapes: lines,

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Daniel Solano Gomez
On Tue Mar 15 09:21 2011, Alan wrote: > On Mar 14, 8:15 pm, Daniel Solano Gomez wrote: > > I believe there are two approaches to doing this in Clojure: > > > > 1. Multimethods:http://clojure.org/multimethods > > 2. Protocols:http://clojure.org/Protocols > > > > Of the two, as of Clojure 1.2, proto

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Alan
On Mar 14, 7:54 pm, stu 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 ...) >

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Alan
Wow, it's like I'm reading Let Over Lambda all over again :) On Mar 15, 9:58 am, Armando Blancas wrote: > 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] >     (ca

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Armando Blancas
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) :

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-15 Thread Alan
On Mar 14, 8:15 pm, Daniel Solano Gomez wrote: > I believe there are two approaches to doing this in Clojure: > > 1. Multimethods:http://clojure.org/multimethods > 2. Protocols:http://clojure.org/Protocols > > Of the two, as of Clojure 1.2, protocols are the preferred way of doing > things.   Wha

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-14 Thread Mark Engelberg
If the set of types is closed and will not be extended by users, there's nothing wrong with just writing your own dispatch using cond, something like: (defn draw [shape] (cond (triangle? shape) (draw-triangle shape) (circle? shape) (draw-circle shape) ...)) Then just write your

Re: Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-14 Thread Daniel Solano Gomez
On Mon Mar 14 19:54 2011, stu wrote: > The problem I have is with the myshapes.picture/draw function. As a > Clojure newb I keep wanting to think of this like a polymorphic > function in the O-O world that relies on each sequence member having a > draw function. > > What's the idiomatic way of han

Polymorphic functions in Clojure (or how to stop thinking in objects)...

2011-03-14 Thread stu
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 thin