I've been toying with something similar. The approach I took was to
define multi-methods for the various operators:

(defmulti add class)
(defmulti sub class)
(defmulti mul class)
(defmulti div class)
 ...

Then I of course write the implementations for various different
types. Then, because the multi-method names don't look nice, I wrote a
macro which transformed the regular operators into calls on the
multi-methods, i.e.:

 (macroexpand-1 '(extended-math (* 3 A))) --> (mul 3 A)

Its still not as clean as a having the operators defined as
multi-methods from the get-go, but you don't have to muck with
overriding the core names.

One reason to leave the core operators alone is performance.
Multi-method dispatch is far more expensive than a regular dispatch. I
don't have this in front of me to get some real numbers, but when I
tested, the same operations with the multi-methods were about 10x
slower than the direct dispatch.

/mike.

On Mon, Dec 8, 2008 at 1:50 PM, ppierre <[EMAIL PROTECTED]> wrote:
>
> On 8 déc, 17:14, Mark Fredrickson <[EMAIL PROTECTED]>
> wrote:
>> >> Alternatively, could I provide a "multi-
>> >> math" lib to redefine the core math functions?
>>
>> > Type classes would be king.
>>
>> Do you mean this in the Haskell sense?
> Yes :  http://www.haskell.org/tutorial/classes.html
> Haskell make a dispatch on type at runtime.
>
>> > (ns test.test
>> >  (:refer-clojure :exclude [+ -]))
>>
>> > (defn +
>> >  [a b]
>> >  33)
>>
>> > (+ 1 2) ;= 33
>>
>> Thanks for the suggestion,
>
> You can "overload" core function :
> (binding [clojure.core/compare (constantly 1)]
>  (sort [1 5 2 6])) ;(6 2 5 1)
>
> But it wouldn't work  if the function is inline.
> (binding [clojure.core/= (constantly false)]
>  [(= 1)
>   (= 1 1)
>   (not= 1 1)
>  ])  ; [false true false]
>
>> this will give me a chance to play around
>> with multi-method and see what I think.
>
> I'm not sure, but :
> defmethod macro simply call addMethod on clojure.lang.MultiFn object.
> So you can add method from an other namespace.
>
> Basically, you would build something like type class.
> But it wouldn't play nice with core operator.
>
> pierre
> >
>

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