[Issue 4725] New: std.algorithm.sum()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Aug 25 18:10:50 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4725

           Summary: std.algorithm.sum()
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: bearophile_hugs at eml.cc


--- Comment #0 from bearophile_hugs at eml.cc 2010-08-25 18:10:40 PDT ---
Writing Python code shows that computing the sum of a sequence of int/FP values
is a very common operation. In D you may write it as:

import std.stdio: writeln;
import std.algorithm: reduce;
void main() {
    auto arr = new double[10];
    arr[] = 1.0;
    auto t = reduce!q{a + b}(arr);
    writeln(t);
}



But I suggest to create std.algorithm.sum() as shorthand, because reduce
requires a bit of cognitive burden that is out of place for this so common and
basic operation:

import std.stdio: writeln;
import std.algorithm: sum;
void main() {
    auto arr = new double[10];
    arr[] = 1.0;
    auto t = sum(arr);
    writeln(t);
}


As second optional argument sum() may accept the starting value:
auto total = sum(range, 0.0);

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list