std.array suggestion

Sean Kelly sean at f4.ca
Thu Mar 9 10:33:57 PST 2006


Don Clugston wrote:
> Oskar Linde wrote:
>> Hello,
>>
>> With the new IFTI support I have been looking at ways of upgrading the 
>> standard library with new and more generic functions. 
> 
> [snip]
>  > Is there in general even any interest in adding generic functions to the
>  > standard library?
> 
> Some of these functions are the last thing remaining in std.math2 (but 
> they definitely don't below there, none of them are truly mathematical). 
> We definitely want to remove std.math2 prior to 1.0, std.array sounds 
> good to me.
> 
> I'll just comment on one function:
> 
>  > T sum(T[] arr)
>  >
>  > Returns the sum of the element in arr as defined by the + operator.
> 
> This one has an interesting twist. I'm not sure that the sum should 
> necessarily be of type T. I've been playing around with the concept of 
> what I've called the Archetype of a type, which is the largest type with 
> the same semantics as T (possibly with the same calculation speed). (ie,
> Archetype!(byte)= Archetype!(short)
> Archetype!(int) = long
> Archetype!(float) = Archetype!(double) = real,
> Archetype!(cfloat)= creal, etc). Obviously it's a trivial template.

Interesting idea.  I've used "LargerOf" templates for min/max 
comparisons before:

template min(T,U) { LargerOf!(T,U) min( T t, U u ) {} }

but the idea of Archetypes is a nice abstraction of this idea.

> I think that at least,  sum(double[] ) should internally use a real 
> while accumulating the sum, so that it can satisfy this test (at least 
> on x86 platforms)

Agreed.

> However, all the other functions seem to be free of mathematical 
> subtleties. sum() is the only one which involves arithmetic operators, 
> and therefore it might not belong with the rest.

sum almost seems out of place here, except that it's an array algorithm. 
  I wonder if it might be more appropriate to create a numerics module 
for this sort of thing?


Sean



More information about the Digitalmars-d mailing list