[Issue 12173] New: Optional start value for std.algorithm.sum
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Sat Feb 15 02:30:00 PST 2014
https://d.puremagic.com/issues/show_bug.cgi?id=12173
Summary: Optional start value for std.algorithm.sum
Product: D
Version: D2
Platform: x86
OS/Version: Windows
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 2014-02-15 02:29:57 PST ---
The built-in Python sum() function supports a second optional argument that is
the start, it's useful when you want to sum an iterable of values starting from
a seed of another type (of different from zero):
>>> sum([1, 2, 3])
6
>>> sum([1, 2, 3], 2)
8
>>> sum([1, 2, 3], 0.0)
6.0
>>> sum([[1, 2], [3]], [])
[1, 2, 3]
This shows one use case for the same functionality in D:
import std.algorithm: sum, reduce;
import std.functional: curry;
struct Foo {
ubyte x;
alias x this;
}
alias mySum = curry!(reduce!q{a + b}, 0);
void main() {
Foo[] arr;
arr.mySum; // OK
arr.sum; // Error
//arr.sum(0); // Not supported
}
dmd 2.065beta3 gives:
..\dmd2\src\phobos\std\algorithm.d(1087,19): Error: cannot implicitly convert
expression (0) of type int to Foo
--
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
More information about the Digitalmars-d-bugs
mailing list