March 25, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4725



--- Comment #19 from bearophile_hugs@eml.cc 2013-03-25 10:49:55 PDT ---
Is it worth adding to sum() an optional argument with the start value, as in
the Python sum()?

In this program in the first case there is an array of floats and you want to use a real sum for max precision. In the second example there is an array of ints, and the programmer wants to sum inside a long to avoid overflow:


import std.stdio, std.algorithm;

void main() {
    float[] a1 = [1.21, 1.3, 1.4];
    real s1 = reduce!q{a + b}(0.0L, a1);
    writefln("%.19f", s1);
    writefln("%.19f", a1.sum);

    int[] a2 = [int.max, int.max, int.max];
    real s2 = reduce!q{a + b}(0L, a2);
    writeln(s2);
    writeln(a2.sum);
}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
May 04, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4725



--- Comment #20 from bearophile_hugs@eml.cc 2013-05-04 13:27:36 PDT ---
An use case. Given some permutations of the chars "ABCD" this program finds the missing one:


import std.stdio, std.string, std.algorithm, std.conv, std.range;

void main() {
    const perms = "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC
                   BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD
                   BADC BDAC CBDA DBCA DCAB".split;

    immutable rowSum = perms[0].reduce!q{a + b};

    foreach (immutable i; 0 .. perms[0].length) {
        immutable sumColumns = reduce!q{a + b}(0, perms.transversal(i));
        write(cast(char)(rowSum - sumColumns % rowSum));
    }
    writeln;
}


Output: DBAC


Using a sum() function:


import std.stdio, std.string, std.algorithm, std.conv, std.range;

void main() {
    const perms = "ABCD CABD ACDB DACB BCDA ACBD ADCB CDAB DABC
                   BCAD CADB CDBA CBAD ABDC ADBC BDCA DCBA BACD
                   BADC BDAC CBDA DBCA DCAB".split;

    immutable rowSum = perms[0].sum(0);

    foreach (immutable i; 0 .. perms[0].length) {
        immutable sumColumns = perms.transversal(i).sum(0);
        write(cast(char)(rowSum - sumColumns % rowSum));
    }
    writeln;
}


As in the Python sum() I have added a seed value, in this case the 0 int.

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4725



--- Comment #21 from bearophile_hugs@eml.cc 2013-06-08 15:15:10 PDT ---
Fortran has a built in sum() that supports a second useful optional argument, that specifies the dimension along to compute the sum, it's useful for 2D or nD arrays:

ke = 0.5d0 * dot_product(mass, sum(v ** 2, dim=1))

See:
http://gcc.gnu.org/onlinedocs/gfortran/SUM.html

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
June 08, 2013
http://d.puremagic.com/issues/show_bug.cgi?id=4725



--- Comment #22 from Andrei Alexandrescu <andrei@erdani.com> 2013-06-08 15:24:12 PDT ---
Time to merge the pull request that's rotting in there...

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
1 2 3
Next ›   Last »