my ideas for array operations

David Brown dlang at davidb.org
Sun Oct 7 14:57:39 PDT 2007


On Sun, Oct 07, 2007 at 10:51:23PM +0100, Janice Caron wrote:
>On 10/6/07, Bill Baxter <dnewsgroup at billbaxter.com> wrote:
>> Here's a 10min prototype:
>> <snip>
>
>>      writefln( accum!("+",int)([1,2,3,4,5]) );
>>      writefln( accum!("*",int)([1,2,3,4,5]) );
>
>Granted that was a ten minute prototype, but please allow me to point
>out that it falls down with zero-element arrays.
>
>int[] a;
>accum!("+",int)(a) // should evaluate to 0
>accum!("*",int)(a) // should evaluate to 1
>
>I'd still vote no to the original idea though, because the language
>can already do what is required. As many others have pointed out, it
>would be easy to write functions like sum() and product() - and to
>templatise them for any type, and to be honest, something like

This operation (usually called 'fold') is very useful in a functional-style
of programming.  Although this can be done, to some extent, in D, it
probably isn't all that common.  Haskell provides numerous fold operations
on whether the operator is applied from the left or the right, and how the
zero term is handled (in fact, it provides an entire module of utilities,
along with a "Class", which is kind of like an interface, for things that
are foldable).

For the zero term, you can either provide what the zero element is, or
disallow the zero element case.

But, I agree, this doesn't need to be in the language, since it is easy to
write.

Dave



More information about the Digitalmars-d mailing list