Bug 4729 – std.algorithm: strange iota behaviour

Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
Other
OS
All
Creation time
2010-08-26T05:16:00Z
Last change time
2013-03-12T21:02:09Z
Assigned to
andrei
Creator
necroment

Comments

Comment #0 by necroment — 2010-08-26T05:16:40Z
import std.stdio, std.algorithm, std.range; void main() { filter!((int n) { write(n, ' '); return 0; })(iota(1, 10)); //writes: 1 2 3 4 5 6 7 8 9 writeln(); reduce!"0"( filter!((int n) { write(n, ' '); return 1; })(iota(1, 10)) ); //writes: 1 2 3 4 5 6 7 8 9 writeln(); reduce!"0"( filter!((int n) { write(n, ' '); return 0; })(iota(1, 10)) ); //Never stops writing numbers }
Comment #1 by bearophile_hugs — 2010-08-26T06:04:08Z
It seems a iota() problem. import std.algorithm: reduce, filter; import std.range: iota; void main() { reduce!"0"(filter!((int a){ return false; })(iota(1))); }
Comment #2 by necroment — 2010-08-26T07:50:27Z
This bug was introduced in 2.048, as is 2.047 iota stops but fails shortly after: src/phobos/std/algorithm.d(279): Enforcement failed
Comment #3 by eco — 2012-02-01T13:21:38Z
2.057 seems to handle this situation (trying to reduce an empty range but not offering a seed) by throwing an appropriate exception: object.Exception@C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(662): Cannot reduce an empty range w/o an explicit seed value. I believe this should be closed. Also, not sure why this is set as an optlink issue.
Comment #4 by hsteoh — 2013-01-03T20:24:38Z
In latest git head, throws an exception. Is this acceptable (should the bug be closed)? Or is it better to have reduce return (ElementType!R).init?
Comment #5 by bearophile_hugs — 2013-01-04T17:53:04Z
(In reply to comment #4) > In latest git head, throws an exception. Is this acceptable (should the bug be > closed)? > > Or is it better to have reduce return (ElementType!R).init? The reduce of Python returns the start value: >>> reduce(lambda a,b: a * b, [], 0) 0
Comment #6 by hsteoh — 2013-01-04T18:17:16Z
The problem here is that no start value is given.
Comment #7 by bearophile_hugs — 2013-01-04T18:36:53Z
(In reply to comment #6) > The problem here is that no start value is given. In this case Python raises an error. I think this is acceptable: >>> reduce(lambda a,b: a * b, []) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: reduce() of empty sequence with no initial value
Comment #8 by hsteoh — 2013-01-04T22:11:08Z
In that case, we should probably close the bug, since the latest Phobos throws an exception upon calling reduce() with an empty range and no start value.
Comment #9 by andrei — 2013-03-12T21:02:09Z
Looks good: exception thrown if no seed, seed returned if it exists.