On 26.02.2009, at 20:18, Phil Hagelberg wrote:
> One approach that's been proposed in #clojure is to make these
> functions
> more capable by default, but then provide a fast-math library that
> could
> redefine them in terms of numerics-only. I'm a big fan of functions
> doing the most helpful thing by default but being able to offer better
> speed when you need it.
>
> Convenience vs speed is always a trade-off, but I think convenience
> should win in the default case. What do others think about this?
My first reaction was "I agree". Speaking in purely abstract terms, I
also prefer convenience to speed as a default. But then I started
thinking a bit about the concrete case you are proposing.
First of all, it's a minor point in Lisp languages. There is nothing
special to functions like +. If you want another + than the one in
clojure.core, all it takes is some manipulation of the use, require,
and refer declarations at the top of your library or script. We are
not in the same situation as languages where there is a small number
of special operators. Nor is + so frequently used in other core
functions that having a second implementation would be pointless.
Second, it is not at all trivial to come up with good scheme for
polymorphic arithmetic operators. Given
(+ a b c d)
with a b c d of four different types, how do you dispatch to a
specific implementation? Dispatch on the type of the first argument?
The first two, with longer argument lists handled by reduction?
Introduce a priority hierarchy in the implementations? Or a
capability list? Among other criteria, it depends on what features of
addition you want to guarantee. Many people would expect
commutativity, based on their daily life experience with numbers, but
that's not easy to guarantee, and it would rule out using addition
for string concatenation, for example. (This is not to say that I am
in favour of string "addition", quite on the contrary, it's just a
nice illustration.)
I propose that instead of discussing that the default implementation
should be, we first start writing a "generic arithmetic" library
module in clojure.contrib and see how it works out in practice. I
expect the main effort to be test-driving the module rather than
implementing it, as the design choices are critical and probably much
harder than the implementation. Any volunteers?
Konrad.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---