Skip to content

Commit

Permalink
Merge pull request #2071 from monarchdodra/12434
Browse files Browse the repository at this point in the history
Fix Issue 12434 - std.algorithm.sum of immutable array too
  • Loading branch information
AndrejMitrovic committed Apr 7, 2014
2 parents abcf061 + 6123ca3 commit a3269b7
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions std/algorithm.d
Expand Up @@ -1090,10 +1090,10 @@ if (isInputRange!R && !isInfinite!R && is(typeof(r.front + r.front)))
{
alias E = Unqual!(ElementType!R);
static if (isFloatingPoint!E)
typeof(E.init + 0.0) seed = 0; //biggest of double/real
alias Seed = typeof(E.init + 0.0); //biggest of double/real
else
typeof(r.front + r.front) seed = 0;
return sum(r, seed);
alias Seed = typeof(r.front + r.front);
return sum(r, Unqual!Seed(0));
}
/// ditto
auto sum(R, E)(R r, E seed)
Expand Down Expand Up @@ -1216,6 +1216,24 @@ unittest
assert(sum(SList!double(1, 2, 3, 4)[]) == 10);
}

unittest // 12434
{
immutable a = [10, 20];
auto s1 = sum(a); // Error
auto s2 = a.map!(x => x).sum; // Error
}

unittest
{
import std.bigint;
immutable BigInt[] a = BigInt("1_000_000_000_000_000_000").repeat(10).array();
immutable ulong[] b = (ulong.max/2).repeat(10).array();
auto sa = a.sum();
auto sb = b.sum(BigInt(0)); //reduce ulongs into bigint
assert(sa == BigInt("10_000_000_000_000_000_000"));
assert(sb == (BigInt(ulong.max/2) * 10));
}

/**
Fills $(D range) with a $(D filler).
*/
Expand Down

0 comments on commit a3269b7

Please sign in to comment.