On Thursday, October 25, 2012 9:16:58 AM UTC-7, Jim foo.bar wrote:
>
> On 25/10/12 16:59, Brian Craft wrote: 
> > I have a fairly common scenario where I have a set of operations that 
> > need to work on two types of data (not "data types" in the clojure 
> > sense) that have different internal structure (i.e. maps with 
> > different keys). I could write a generic function that operates on 
> > both types of map. 
>
> I'm not sure I follow why you need polymorphic behavior of any 
> form...the operations that need to operate on 2 internally different 
> maps can take the keys to 'touch' as parameters couldn't they? Then it 
> is truly generic...if this is not practical you can always make a HOF 
> that returns the appropriate handler for the data-type you're examining 
> at any given time...meta-data could help you distinguish between the 
> maps... 
>

Is this reimplementing a small bit of what multimethods do? I manually 
write something to keep track of handlers, examine the data type of 
parameters, and then dispatch? How is that different than a multimethod?

Hm... I'm trying to get more specific w/o getting lost in details. Briefly, 
I'm drawing a graphic. The graphic can have different kinds of objects 
which define the x axis. In one form, it's a list of objects each of which 
includes a numeric range of sub-elements. E.g. [{:name 'A :start 1 :end 10} 
{:name 'B :start 1 :end 30} {:name 'C :start 200 :end 400}]. In another 
form it's a list of objects each of which includes a list of objects, e.g. 
[{:name 'A :elements ['a 'b 'c]} {:name 'B :elements ['d 'e 'f]}].

On mouse events, corresponding elements on the x axis have to be found, but 
the way you do this is different for the two types. A drag-zoom handler 
might look something like this:

(def drag-zoom [xaxis x1 x2]
   (set-position (interval xaxis (find-element xaxis x1) (find-element 
xaxis x2))))

where find-element finds the element at the given coord (e.g. 'A 'b in one 
mode, or 'A 8 in the other), interval returns an sub-interval on the xaxis 
(like [{:name 'A :elements ['b 'c]} {:name 'B :elements ['d 'e]}], or 
[{:name 'A :start 8 :end 10} {:name 'B :start 1 :end 10}], and set-position 
updates the current position with the sub-interval.


But, in general, I think any classic OOP example would do. You have 12 
types of shapes and a method total-area that finds the sum of the areas of 
all the instantiated shapes. If you use maps to represent the shapes, where 
do you implement the area calculations for the 12 types of shapes? A 
multimethod that inspects a :shape key? Or convert to records? I could do 
it javascript functional style and write a function that returns a dispatch 
function representing a particular shape, and which implements an 'area' 
method. But that's getting very OOP, and I doubt it's idiomatic clojure.

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
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