[Issue 10670] New: std.algorithm.reduce: no-seed initialization wrong design
d-bugmail at puremagic.com
d-bugmail at puremagic.com
Fri Jul 19 03:07:11 PDT 2013
http://d.puremagic.com/issues/show_bug.cgi?id=10670
Summary: std.algorithm.reduce: no-seed initialization wrong
design
Product: D
Version: D2
Platform: All
OS/Version: All
Status: NEW
Severity: normal
Priority: P2
Component: Phobos
AssignedTo: monarchdodra at gmail.com
ReportedBy: monarchdodra at gmail.com
--- Comment #0 from monarchdodra at gmail.com 2013-07-19 03:07:10 PDT ---
reduce has a "no-seed" implementation provided: "The one-argument version $(D
reduce!fun(range)) works similarly, but it uses the first element of the range
as the seed (the range must be non-empty)."
I question this design as invalid: The first element is never passed through
the transform function. There is no reason it be given any "special treatment".
Here is a trivial example of why the design is (IMO) wrong:
//----
import std.stdio, std.algorithm, std.range;
void main(string[] args)
{
auto arr = [1, 1];
auto sumDoubles1 = reduce!"a + 2*b"(0, arr);
auto sumDoubles2 = reduce!"a + 2*b"(arr);
writeln(sumDoubles1); //4
writeln(sumDoubles2); //3 (!)
}
//----
I really can't see any universe where you could justify that return value...
The correct seed value should be "Seed seed = fun(Seed.init, r.front)": This
means the first element is correctly "processed". Of course, doing this really
just boils down to seed being Seed.init.
An added "bonus" is that it makes simple "isIterable" types much more
efficient, as they don't have to check "isInitialized" on every iteration.
--------
Unsure if this is "ER", "wrong-code" or what. Not sure if changing the behavior
is acceptable (IMO, it should be).
I was re-writing reduce already, so please provide your feedback on the issue
so that I can take it into account :)
--
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